I am trying to set cookie using django set_cookie.
I am converting dict to string and then setting it in a cookie named ‘blah’.
The cookie gets set, but I see that the commas are replaced with \054.
Python code
x = {
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
response.set_cookie('blah', json.dumps(x))
return response
How I see it in chrome:
"{\"key1\": \"value1\"\054 \"key2\": \"value2\"\054 \"key3\": \"value3\"}"
Any pointer what am I missing – please suggest.
django==4.2
>Solution :
Any pointer what am I missing – please suggest.
Nothing. Indeed, this is exactly the same as a comma. Indeed, '\054' == ',':
> '\054' == ','
true
It is just how it shows a string, but the content is exactly the same. Indeed, if we thus would parse the JSON blob, we get:
> JSON.parse("{\"key1\": \"value1\"\054 \"key2\": \"value2\"\054 \"key3\": \"value3\"}");
{ key1: 'value1', key2: 'value2', key3: 'value3' }
So it parses it to the right JavaScript object. You can compare it to a string literal with 'foo' and "foo", both are presentations of the same value, just like '\40' and ' ' are the same as well.