Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django is adding quotes to cookies with colons

I run:

response.set_cookie(key, value=value, expires=expires, path=path, domain=domain)

When the value is: aa:aa
The cookie value is: "aa:aa"

When the value is: aa
The cookie value is: aa

I need to prevent django from adding quotes when there are colons in the value

like image 843
guyyug Avatar asked Nov 12 '14 16:11

guyyug


1 Answers

When setting the value into cookie, just encode it using urllib.parse.quote_plus() or urllib.parse.quote(). e.g.

value = urllib.parse.quote_plus(value)
response.set_cookie(key, value=value, expires=expires, path=path, domain=domain)

And while getting value from cookie, decode it back using urllib.parse.unquote_plus() or urllib.parse.unquote() respectively. e.g.

if key in request.COOKIES:
    value = urllib.parse.unquote_plus(request.COOKIES[key])
like image 141
OM Bharatiya Avatar answered Oct 23 '22 20:10

OM Bharatiya