[Zope] Cookies and dictionaries

Dieter Maurer dieter at handshake.de
Sat Feb 7 13:40:21 EST 2004


Kashif Jabbar wrote at 2004-2-6 14:01 -0600:
>I am trying to store a dictionary in a cookie which I would like to 
>retrieve later and use.

You must understand what cookies are. Reading the cookie specification
may help.

   You cannot expect that a Python dictionary used as a cookie value
   comes back as a Python dictionary.

   In fact, Zope transforms it to a string (as it knows, that
   a cookie value must be a string), and it comes back as this
   string (and not the original dictionary).

> ...
>    mydic = request.myCookie
>    print mydic['filter']
>
>return printed
>###########
>
>When the script is run I get an error:
>
>> *Error Type: TypeError*
>> *Error Value: sequence index must be integer*
>
>It seems that the dictionary is returned as a string.
Indeed.

>How can I cast 
>this string to a dictionary type?

You cannot.

You might be able to use "eval" (in an External Method)
to get the original dict back (it will work, if the dictionary
contained only simple objects).
Be warned, however, that "eval" is a dangerous function, when
applied to untrusted strings (such as strings sent as cookies).

-- 
Dieter



More information about the Zope mailing list