oscarmlage oscarmlage

Pyramid: set a cookie returning a HTTPFound

Written by oscarmlage on

Probably it will not be the most common scenario, but if you want to save a cookie and make a redirection right away using HTTPFound, the cookie won't being saved. That's the fact.

It seems that HTTPFound takes a own headers parameter, probably overwritting the one you used to set the cookie, so theorically nothing happened with your response.set_cookie() unless you call HTTPFound with the proper argument:

response.set_cookie('mycookie', value=myvalue)
return HTTPFound(location=request.environ['HTTP_REFERER']),
                 headers=response.headers)

Thanks to Rob Wouters for the excellent reply in this Stack Overflow thread, it was hard to find it for me, that's the main reason of this post.