Abhay Bhargav
Abhay Bhargav

Reputation: 399

Add parameter to cookie - python requests library

I am using Python's Requests library for some web scraping. I am using the Session() object to retain the session throughout the chain of HTTP GET and POST requests.

I need to make an HTTP POST request like this:

POST http://www.examplesite.com/login HTTP/1.1
Host: www.examplesite.com
Proxy-Connection: keep-alive
Content-Length: 126
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://www.examplesite.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://www.examplesite.com/anon
Accept-Encoding: sdch
Accept-Language: en-US,en;q=0.8
Cookie: SESS01be97003f5147af7927b0548df40bcd=qud5ra51jmtf9opnihn6tct2v0; has_js=1

I need to add the parameter has_js=1 to the cookie object in the Requests session. I am not able to find any info on this specific requirement. Help would be greatly appreciated!

Upvotes: 0

Views: 940

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121764

has_js=1 is just another cookie name-value pair (most likely a Drupal JavaScript probe cookie).

All cookies your browser sends are concatenated in the one Cookie header, separated by ; semicolons. Cookie parameters are part of the Set-Cookie header to influence what the browser does when storing the values, and never part of the Cookie header sent back to the server.

You can just add it to your session:

session.cookies['has_js'] = '1'

Upvotes: 1

Related Questions