How to use cookies in Python Requests

How to use cookies in Python Requests

You can use a session object. It stores the cookies so you can make requests, and it handles the cookies for you

s = requests.Session() 
# all cookies received will be stored in the session object,data=payload)


You can also save the cookie data to an external file, and then reload them to keep session persistent without having to login every time you run the script:

How to save requests (python) cookies to a file?

From the documentation:

  1. get cookie from response

    url =
    r = requests.get(url)

    {example_cookie_name: example_cookie_value}

  2. give cookie back to server on subsequent request

    url =
    cookies = dict(cookies_are=working)
    r = requests.get(url, cookies=cookies)`

How to use cookies in Python Requests

Summary (@Freek Wiekmeijer, @gtalarico) others answer:

Logic of Login

  • Many resource(pages, api) need authentication, then can access, otherwise 405 Not Allowed
  • Common authentication=grant access method are:
    • cookie
    • auth header
      • Basic xxx
      • Authorization xxx

How use cookie in requests to auth

  1. first get/generate cookie
  2. send cookie for following request
  • manual set cookie in headers
  • auto process cookie by requestss
    • session to auto manage cookies
    • response.cookies to manually set cookies

use requestss session auto manage cookies

curSession = requests.Session() 
# all cookies received will be stored in the session object

payload={username: yourName,password: yourPassword}, data=payload)
# internally return your expected cookies, can use for following auth

# internally use previously generated cookies, can access the resources


manually control requestss response.cookies

payload={username: yourName,password: yourPassword}
resp1 =, data=payload)

# manually pass previously returned cookies into following request
resp2 = requests.get(secondUrl, cookies= resp1.cookies)

resp3 = requests.get(thirdUrl, cookies= resp2.cookies)

Leave a Reply

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