Python GAE Requests not returning cookies after get request

1 view
Skip to first unread message

user2656606 via StackOverflow

unread,
Aug 6, 2013, 8:48:24 AM8/6/13
to google-appengin...@googlegroups.com

I have written a project in python which i am now in the process of moving to google app engine. The problem that occurs is when i run this code on GAE

import requests
from google.appengine.api import urlfetch

def retrievePage(url, id):
    response = 'http://online2.citybreak.com/Book/Package/Result.aspx?onlineid=%s' % id
    # Set the timeout to 60 seconds
    urlfetch.set_default_fetch_deadline(60)

    # Send the first request
    r1 = requests.get(url)

    cookies = r1.cookies

    print 'Cookies: %s' % r1.Cookies

    # Retrieve the content
    r2 = requests.get(response, cookies=cookies)
    return r2.text

When running the code on GAE the cookies from the first request are missing. That is to say, r1.Cookies is just an empty cookie jar. The same code works just fine on my django server where the cookies should contain a asp.net session id.

The reason i have two requests is because the first one redirects the user and will only retrieve the correct page if the session cookie is the same.

print output on GAE

Cookies: <<class 'requests.cookies.RequestsCookieJar'>[]>

print output on Django

Cookies: <<class 'requests.cookies.RequestsCookieJar'>[<Cookie ASP.NET_SessionId=dhmk1vt3ujgmhhhmbwsclukb for online2.citybreak.com/>]>

Anyone know what the problem might be? Is GAE stripping away the cookie information? I am also opened to any suggestions on another way to retrieve the page, i just found that the requests module was easier than the alternatives i found.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/18080556/python-gae-requests-not-returning-cookies-after-get-request

user2656606 via StackOverflow

unread,
Aug 6, 2013, 9:08:24 AM8/6/13
to google-appengin...@googlegroups.com

I have written a project in python which I am now in the process of moving to google app engine. The problem that occurs is when I run this code on GAE:

import requests
from google.appengine.api import urlfetch

def retrievePage(url, id):
    response = 'http://online2.citybreak.com/Book/Package/Result.aspx?onlineid=%s
' % id
    # Set the timeout to 60 seconds
    urlfetch.set_default_fetch_deadline(60)

    # Send the first request
    r1 = requests.get(url)

    cookies = r1.cookies

    print 'Cookies: %s' % r1.cookies

    # Retrieve the content
    r2 = requests.get(response, cookies=cookies)
    return r2.text

When running the code on GAE the cookies from the first request are missing. That is to say, r1.cookies is just an empty cookie jar. The same code works just fine on my django server where the cookies should contain a asp.net session id.

Faisal via StackOverflow

unread,
Aug 6, 2013, 10:33:26 AM8/6/13
to google-appengin...@googlegroups.com

I tried urlfetch it seems to be showing the cookie headers:

import logging
from google.appengine.api import urlfetch

response = urlfetch.fetch(url)
logging.info(response.headers)


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/18080556/python-gae-requests-not-returning-cookies-after-get-request/18083081#18083081
Reply all
Reply to author
Forward
0 new messages