How to set a cookie with webapp ?

125 views
Skip to first unread message

Seth

unread,
Apr 8, 2008, 3:06:40 AM4/8/08
to Google App Engine
Hello,

Since webapp seems to use WebOb, and the WebOb Response object has a
set_cookie method, I'm curious why I get the following error:

File "/Users/sethladd/Development/example/app.py", line 41, in post
self.response.set_cookie('nick', account.nick, max_age=360,
path='/', domain='example.com', secure=True)
AttributeError: 'Response' object has no attribute 'set_cookie'

Here's my code:

account.put()
self.response.set_cookie('nick', account.nick, max_age=360,
path='/', domain='example.com', secure=True)
self.redirect('/')

What's the best way to send over a cookie?

Thanks very much

Marzia Niccolai

unread,
Apr 8, 2008, 4:55:16 PM4/8/08
to Google App Engine
Hi Seth,
You can set cookies by adding the cookie to your response header:

self.response.headers.add_header(
'Set-Cookie', 'name=value; expires=%s' % expires_rfc822)

-Marzia

Seth

unread,
Apr 10, 2008, 3:56:42 AM4/10/08
to Google App Engine
Great, thanks. That's what I ended up doing. I think the docs are a
little misleading here. They infer (explicitly mention?) that the
response object is a WebOb object.

Setting the cookie manually works, thanks!

On Apr 8, 10:55 am, ma...@google.com wrote:
> Hi Seth,
> You can setcookiesby adding the cookie to your response header:

ryan

unread,
Apr 10, 2008, 5:23:58 AM4/10/08
to Google App Engine
You might have been thinking of the Request class. It does derive from
webob.Request, but the Response class doesn't derive from
webob.Response. The docs reflect this, but we could consider
emphasizing it more.

http://code.google.com/appengine/docs/webapp/requestclass.html
http://code.google.com/appengine/docs/webapp/responseclass.html

batobin

unread,
Apr 16, 2008, 5:39:15 PM4/16/08
to Google App Engine
How are cookies retrieved after they are stored? I'm trying:

self.response.headers.add_header('Set-Cookie', session_cookie)
cookie_str = self.response.headers.get('Cookie')

But cookie_str is None every time. I can confirm that the cookie is
actually being set.

Thanks in advance,
BT

On Apr 10, 12:56 am, Seth <sethl...@gmail.com> wrote:
> Great, thanks.  That's what I ended up doing.  I think the docs are a
> little misleading here.  They infer (explicitly mention?) that the
> response object is a WebOb object.
>
> Setting thecookiemanually works, thanks!
>
> On Apr 8, 10:55 am, ma...@google.com wrote:
>
> > Hi Seth,
> >  You can setcookiesby adding thecookieto your response header:

manschmidt

unread,
Apr 16, 2008, 6:03:14 PM4/16/08
to Google App Engine
Not 100% sure, but i think a browser is always sending the cookie with
a request if one is set.

just try to analyze the whole get/post-data your script is getting
from the browser.


keep on coding,
manuel

batobin

unread,
Apr 16, 2008, 6:21:06 PM4/16/08
to Google App Engine
The whole thing just seems strange. Coming from a PHP background, GAE
is completely failing me in this respect. PHP comes with $_SESSION.
Boom. Done. And if I set a cookie, I can use $_COOKIE. There must be
an easy/elegant way to grab this data in GAE.

BT

manschmidt

unread,
Apr 16, 2008, 6:25:46 PM4/16/08
to Google App Engine
aaaah i just found your mistake.

its a "request" not a "response"

so it must be:

cookie_str = self.request.headers.get('Cookie')

that works to me

btw, im also just php-coding - but this will change though this
project!
~manuel

batobin

unread,
Apr 16, 2008, 9:23:56 PM4/16/08
to Google App Engine
Niiiiiiiiiiiiiiiiice.

Still, for anyone who doesn't want to use Google accounts, seems like
a lot of work to remember a user from page to page. Can we get a
session api?

BT

On Apr 16, 3:25 pm, manschmidt <google...@tliff.com> wrote:
> aaaah i just found your mistake.
>
> its a "request" not a "response"
>
> so it must be:
>
> cookie_str = self.request.headers.get('Cookie')
>
> that works to me
>
> btw, im also just php-coding - but this will change though this
> project!
> ~manuel
> On 17 Apr., 00:21, batobin <tobinindustr...@gmail.com> wrote:
>
> > The whole thing just seems strange. Coming from a PHP background, GAE
> > is completely failing me in this respect. PHP comes with $_SESSION.
> > Boom. Done. And if I set acookie, I can use $_COOKIE. There must be

Lee O

unread,
Apr 16, 2008, 9:32:40 PM4/16/08
to google-a...@googlegroups.com
I agree completely, If you're using Google Accounts, the user api should handle that by default.. i was honestly shocked when i first found out that it didn't.
--
Lee Olayvar
http://www.leeolayvar.com

Ross Ridge

unread,
Apr 16, 2008, 11:00:40 PM4/16/08
to Google App Engine
Actually, the docs explicity state that the framework uses the WebOb
Response class:

http://code.google.com/appengine/docs/webapp/buildingtheresponse.html
The request handler instance builds the response using its
response property. This is initialized to an empty WebOb Response
object by the application.

If the Response class doesn't derive the WebOb class then
documentation should be updated to both reflect this and to document
what methods and other attributes the class supports.

Ross Ridge

Ian Bicking

unread,
Apr 17, 2008, 5:45:26 PM4/17/08
to google-a...@googlegroups.com

What was the reasoning for that? It seems like it'd be a small change
to use the webob.Response class, with better cookie handling and other
features as a result.

It seems like it's using the start_response writer instead of the
app_iter that webob.Response prefers, but in a CGI context these should
be completely equivalent.

--
Ian Bicking : ia...@colorstudy.com : http://blog.ianbicking.org

Reply all
Reply to author
Forward
0 new messages