As you are probably aware, more often than not, an HTTP server will send more than just a session_id cookie; however, httplib2 handles cookies with a dictionary, like this:
response, content = http.request(url, 'GET', headers=headers)
headers = {'Cookie': response['set-cookie']}
url = 'http://www.example.com/home'
response, content = http.request(url, 'GET', headers=headers)
So, how do I set the extra cookies? If handled with a dictionary, I can't have double Cookie keys :S.
Thanks for your time.
The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later. To send multiple cookies, multiple Set-Cookie headers should be sent in the same response.
If multiple cookies of the same name match a given request URI, one is chosen by the browser. The more specific the path, the higher the precedence. However precedence based on other attributes, including the domain, is unspecified, and may vary between browsers.
@RobDolinMS Also, you can't have multiple cookie headers in the request. As per RFC 6265 S5. 4: When the user agent generates an HTTP request, the user agent MUST NOT attach more than one Cookie header field.
Cookies are contained in a single HTTP header, separated by semicolons. Example:
cookie1=value1;cookie2=value2
So you'll need to build a string from the cookies sent by the server, and then set that as the Cookie
header.
Edit: Actually, playing around a bit with httplib2 and re-reading your question, I'm not sure you actually need to do anything to get the functionality you want. The set-cookie
value you get back from httplib2 is actually the raw Set-Cookie
header sent from the server; you can just put that into the cookie
header of the new response, and everything will work fine. Technically speaking you should remove some cookie attributes such as expiry
, but I imagine most servers will handle that just fine.
Yes, I just found out elsewhere about the Cookie header when making the request, but the server may send several Set-Cookie headers, with a cookie(and expiration,domain,etc attributes) per header. But with the dictionary system used in httplib2, I can't really get all the possible Set-Cookie headers sent by the server, but seemingly, just the last one.
So, any more ideas :)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With