Search code examples
pythontornado

tornado get_secure_cookie returning not the same value that was set


all.

I have following problem with set_secure_cookie/get_secure_cookie functions. In a nutshell I am doing something like this:

username = 'user'
print(username)
self.set_secure_cookie('user', username)
print(self.get_secure_cookie('user'))

and the result is:

user
b'"user"'

What I can't understand is why initial and returned values differ. It's probably has to do something with escaping, but still I cannot catch the reason behind such behavior - typically when you use some Api to store a value you expect that you will retrieve the same value. Why this assumption is wrong in this case?


Solution

  • For some reason get_secure_cookie returns a bytestring instead of a unicode string (I suppose because it leaves it up to client to decide which encoding use). So for for utf-8 latin symbols you can get the initial string by running tornado.escape.json_decode on it.