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?


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:


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.


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) 

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

    In another function:

Leave a Reply

Your email address will not be published. Required fields are marked *