Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Cookies in Session Using python-requests Module

I'm using python-requests module to handle oAuth request and response. I want to set received access_token (response content as dict) in requests.session.cookies object.

How can I update existing cookies of session with received response from server?

[EDIT]

self.session = requests.session(auth=self.auth_params)
resp = self.session.post(url, data=data, headers=self.headers)
content = resp.content

I want to do something like:

requests.utils.dict_from_cookiejar(self.session.cookies).update(content)

Here, requests.utils.dict_from_cookiejar(self.session.cookies) returns dict with one session key. Now, I want to update received response content in self.session.cookies.

like image 215
Yajushi Avatar asked Dec 20 '12 11:12

Yajushi


People also ask

How do you pass a cookie request in Python?

To send a request with a Cookie, you need to add the "Cookie: name=value" header to your request. To send multiple cookies in a single Cookie header, separate them with semicolons or add multiple "Cookie: name=value" request headers.

How do I add cookies to my requests?

To add cookies to a request for authentication, use the header object that is passed to the get/sendRequest functions. Only the cookie name and value should be set this way. The other pieces of the cookie (domain, path, and so on) are set automatically based on the URL the request is made against.

How do you store cookies in Python?

Create cookie In Flask, set the cookie on the response object. Use the make_response() function to get the response object from the return value of the view function. After that, the cookie is stored using the set_cookie() function of the response object. It is easy to read back cookies.


2 Answers

requests can do that for you, provided you tell it all the requests you make are part of the same session:

>>> import requests
>>> s = requests.session()
>>> s.get('https://www.google.com')
<Response [200]>
>>> s.cookies
<<class 'requests.cookies.RequestsCookieJar'>[Cookie(version=0, name='NID'...

Subsequent requests made using s.get or s.post will re-use and update the cookies the server sent back to the client.


To add a Cookie on your own to a single request, you would simply add it via the cookies parameter.

>>> s.get('https://www.google.com', cookies = {'cookieKey':'cookieValue'})

Unless the server sends back a new value for the provided cookie, the session will not retain the provided cookie.

like image 184
Thomas Orozco Avatar answered Sep 24 '22 13:09

Thomas Orozco


In order to provide a cookie yourself to the requests module you can use the cookies parameter for a single request and give it a cookie jar or dict like object containing the cookie(s).

>>> import requests
>>> requests.get('https://www.example.com', cookies {'cookieKey':'cookieValue'})

But if you want to retain the provided cookie without having to set the cookies parameter everytime, you can use a reqests session which you can also pass to other funtions so they can use and update the same cookies:

>>> session = requests.session()
>>> session.cookies.set('cookieKey', 'cookieName')
# In order to avoid cookie collisions
# and to only send cookies to the domain / path they belong to
# you have to provide these detail via additional parameters
>>> session.cookies.set('cookieKey', 'cookieName', path='/', domain='www.example.com')
like image 42
Semnodime Avatar answered Sep 25 '22 13:09

Semnodime