Update Cookies in Session Using python-requests Module
Asked Answered
A

3

7

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.

Anorexia answered 20/12, 2012 at 11:54 Comment(0)
A
1

This code worked for me. hope it can help to someone else.

I want to update session.cookies variable with received response values from post request. so, same request value can be used in another post/get request.

here, what I did:

1) updated requests module to 1.0.3 version.

2) created 2 functions

   session = requests.session() 
   def set_SC(cookie_val):
            for k,v in cookie_dict.iteritems():
                if not isinstance(v, str):
                    cookie_dict[k] =  str(v) 
            requests.utils.add_dict_to_cookiejar(session.cookies,
                                                 cookie_val)

    def get_SC():
            return requests.utils.dict_from_cookiejar(session.cookies)

    In another function:
    setSC(response.content)
Anorexia answered 21/12, 2012 at 12:33 Comment(1)
This sound like a workaround, i.e. why do request doesn't save cookies of a post call ? see ietf.org/rfc/rfc2109.txt in section 5.1 example. I'll raise it to requestsRioux
H
4

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.

Houphouetboigny answered 20/12, 2012 at 12:3 Comment(7)
thanks, how it will work for post request. can you give example ?Anorexia
I'm getting {'access_token':'Somecode'} in response, I want to set the same dict in existing session object's cookies. after getting response. How can I achieve this ?Anorexia
@Anorexia You don't need to, so long as you use a session object, requests will re-use the cookies in subsequent requests automatically. For the post request, do: s.post(url, data = {k:v})Houphouetboigny
but, I want to set response content (from post request) in session.cookies.Please check edit. Please, correct me if I misunderstood.Anorexia
Thank You for quick reply. for me, it wasn't updating cookies automatically specially for post request. please check my answer, it worked for me.Anorexia
Subsequent requests do not seem to reuse the same cookies.Reconstructionism
Incorrect information. Passed parameters are not persistent. And this doesn't solve the issue over updating cookies.Cuisine
H
2

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')
Hautboy answered 11/6, 2018 at 18:6 Comment(0)
A
1

This code worked for me. hope it can help to someone else.

I want to update session.cookies variable with received response values from post request. so, same request value can be used in another post/get request.

here, what I did:

1) updated requests module to 1.0.3 version.

2) created 2 functions

   session = requests.session() 
   def set_SC(cookie_val):
            for k,v in cookie_dict.iteritems():
                if not isinstance(v, str):
                    cookie_dict[k] =  str(v) 
            requests.utils.add_dict_to_cookiejar(session.cookies,
                                                 cookie_val)

    def get_SC():
            return requests.utils.dict_from_cookiejar(session.cookies)

    In another function:
    setSC(response.content)
Anorexia answered 21/12, 2012 at 12:33 Comment(1)
This sound like a workaround, i.e. why do request doesn't save cookies of a post call ? see ietf.org/rfc/rfc2109.txt in section 5.1 example. I'll raise it to requestsRioux

© 2022 - 2024 — McMap. All rights reserved.