Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

httplib2, how to set more than one cookie?

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.

like image 658
user168833 Avatar asked Nov 15 '09 17:11

user168833


People also ask

Can you have multiple set-cookie?

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.

Can there be 2 cookies with same name?

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.

Can you have multiple cookie headers?

@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.


2 Answers

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.

like image 133
ShZ Avatar answered Sep 30 '22 19:09

ShZ


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 :)?

like image 30
user168833 Avatar answered Sep 30 '22 20:09

user168833