Update Cookies in Session Using python-requests Module

后端 未结 3 1870
陌清茗
陌清茗 2021-01-18 23:32

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

3条回答
  •  粉色の甜心
    2021-01-18 23:56

    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)
    

提交回复
热议问题