Reputation: 50362
Been banging my head against the well on this issue for a few hours. I just can't get cookies set properly. The cookie that I am setting never seems to get saved or sent back properly.
The situation is that the cookie:
Server: "webapp" on "google app engine"
Client: Chrome browser, Javascript, jQuery, Ajax calls
Using the following ajax to log the user in, this should set a cookie with a "token":
$.ajax({
type: 'POST',
url: '/rest/login/',
data: JSON.stringify({username:username, password: password}),
error: function(jqXHR, status, errorThrown){...},
success: function(data, status, jqXHR){...}
});
This produces the following headers larger pic:
The server is running webapp on google app engine, this is how it sets the cookie:
w_self.response.set_status(200)
# Put the token in the cookies. "str()" is used because without it, there is a unicode error
w_self.response.headers.add_header(str('Set-Cookie'), str('token=%s; max_age=360;' % token))
# The user
r_object['user'] = the_user
# Respond
w_self.response.out.write(json.dumps(r_object))
If this page is requested again, the cookie IS sent back to the server larger pic:
But does not appear to be saved anywhere because I can never find it when exploring cookies in the developer tools larger pic:
It also is not sent when requesting a resource who's path is not exactly the same ('logout' instead of 'login':
$.ajax({
type: 'POST',
url: '/rest/logout/',
data: JSON.stringify({something:'else'}),
error: function(jqXHR, status, errorThrown){...},
success: function(data, status, jqXHR){...}
});
This is what the request looked like:
Upvotes: 2
Views: 1285
Reputation: 11992
I'm not sure why setting the headers isn't working, maybe you could try using the set cookie method:
# Saves a cookie in the client.
response.set_cookie('some_key', 'value', max_age=360, path='/',
domain='example.org', secure=True)
http://webapp-improved.appspot.com/guide/response.html#setting-cookies
Here's the source for the set_cookie
method:
Upvotes: 1
Reputation: 10360
If you don't set a path, then the default is to only send it back on the path that set it, as you're seeing.
I also think it's also meant to be Max-Age rather than max_age as you have it, but regardless the response objects that webapp2 give you have a set_cookie method though - I'm sure that will do it all properly.
Upvotes: 1