Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not able to set cookie using django set_cookie

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"{\"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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading