Search code examples
pythongoogle-app-enginesessionwebapp2

Getting error while trying to add header with Set-Cookie in GAE


I am trying to include external python module in my project for working with sessions. It's named gmemsess.py. It tries to add Set-Cookie header in the response and an error appears:

rh.response.headers.add_header('Set-Cookie','%s=%s; path=/;'%(name,self._sid))
AttributeError: HeaderDict instance has no attribute 'add_header'

I read documentation and everything seems ok, but it doesn't work. Why this error can appear? Also, I use webapp2 to manage subdomains. May be there is something goes wrong because of this?


Solution

  • The headers.add_header method should absolutely work if you are using stock AppEngine, but I am guessing that you are using a framework -- and there are plenty of them, like Bottle -- that uses a custom replacement for webob's Response object.

    A little bit of time with Google reveals that there is at least one identifiable class called HeaderDict that extends MultiDict, and I think that is what you are dealing with. In that case, you should to into gmemsess.py and change the line

    rh.response.headers.add_header('Set-Cookie','%s=%s; path=/;'%(name,self._sid))
    

    to read

    rh.response.headers['Set-Cookie'] = '%s=%s; path=/;'%(name,self._sid)
    

    That should fix you right up.