[mod_python] getting cookies from the response

Graham Dumpleton grahamd at dscpl.com.au
Sat Nov 18 01:43:08 EST 2006

On 18/11/2006, at 4:46 AM, Istvan De wrote:

> Hi Graham,
> On Fri, 2006-11-17 at 20:21 +1100, Graham Dumpleton wrote:
>> On 17/11/2006, at 8:09 PM, Istvan De wrote:
>>> Hi!
>>> In my code, if I detect that a cookie is not set, I set it using
>>> Cookie.add_cookie().
>>> That is all well. However, if I read this cookie from the same  
>>> request
>>> at which it was added, it is not available. I guess this is because
>>> the
>>> "cookie add" function puts the cookie into the reponse of the page,
>>> and
>>> the "cookie get" function only looks at the request to see what
>>> cookies
>>> are set.
>>> What is the solution to my problem?
>> Cookie.get_cookies() reads from req.headers_in. Cookie.add_cookie()
>> writes to req.headers_out. Thus, you can't read back what you only  
>> just
>> wrote.
>> Suggest you log using apache.log_error() the values of the input and
>> output headers to see what they are set to.
>> Note that Cookie.add_cookie() must be called prior to the first time
>> that
>> any actual content is written back as the response.
> Yes, these are all clear to me. But what do you advise as the solution
> to the problem? I need to read some cookies just created. Or maybe
> re-load the page with a meta tag after creating the cookies? But this
> would be sort of a hack.

Please keep discussion on the mailing list.

It is unclear why you would want to do it in the first place which  
suggests that whatever you are doing you are going about it all in the
wrong way. Can you explain what problem it is you are really trying to

Anyway, req.headers_in should be writable and so you could always
create the 'cookie' header and resave it. That can be a bit tricky  
if you are getting sent multiple cookies as you potentially have to  
it back in with those other cookies.


More information about the Mod_python mailing list