Update Cookies in Session Using python-requests Module

后端 未结 3 1901
陌清茗
陌清茗 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:52

    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')
    

提交回复
热议问题