Python Requests Cookie: Handling Cookies in HTTP Requests
PythonLearn how to handle cookies in HTTP using the Python Requests library. Understand cookie storage, sessions, and how to send, read, save, and load cookies.

Kazys Toleikis
Key Takeaways
-
The requests library lets you inspect and send cookie values easily using cookies and response.cookies.
-
A session object persists cookie data across multiple Python requests, making it ideal for flows that rely on authentication.
-
You can save cookies to disk and load cookies later to restore session state without logging in again.
Cookies are key parts of how the Python Requests library, and the rest of the internet, handle state and identity in web communication. They store small bits of data that link different parts of a user’s interaction with a website, which helps servers recognize the same client across multiple requests.
Developers rely on cookies for login sessions, user tracking, preferences, and more. When working with the Requests library, managing cookie values properly ensures smooth access to protected resources and stable connections with any API endpoint that expects persistent session data.
What Are Cookies and Why They Matter in HTTP
Cookies are small pieces of data that a server sends to a client during a request. The client stores these cookies and sends them back on future requests. It allows the server to remember you.
These cookies often drive authentication, where the server knows who you are after you log in. They also support sessions, letting you stay logged in across several requests. Additionally, they enable personalization, and the server remembers your preferences or items in a shopping cart.
Since cookies link several requests to the same user or browser state, they matter for any system that isn’t purely stateless.
How Python Requests Handles Cookies
With Python Requests , cookies are supported out of the box. When you use import requests, you gain access to a response object that has a cookies attribute.
The response.cookies attribute returns a RequestsCookieJar, containing the cookie data sent by the server. What’s more, the library offers a session object via requests.Session().
The object persists cookie session data between multiple requests: all cookie values returned by one request will be available for the next, when using the same session.
In short, the Requests library stores and manages cookies automatically when you use a session object, and lets you inspect data via response.cookies.
Sending Cookies With Requests
You can manually send cookies with a request by using the cookies parameter on a request method. It lets you explicitly pass in cookie data when making the request. For example:
import requests
url = "https://httpbin.org/cookies"
headers = {"User-Agent": "my-app/1.0"}
custom_cookies = {"session_id": "abc123", "theme": "dark"}
response = requests.get(url, headers=headers, cookies=custom_cookies)
Here, you provided the session_id via cookies, while the User-Agent was correctly sent as a header. The server will receive the cookie values as if they were set previously.
It means you control what cookie session data you send, instead of relying on what the server has set for you.
Reading Cookies From a Response
Once you perform a request, you get a response object, and you can inspect the data returned by the server. For example:
import requests
response = requests.get("https://httpbin.org/cookies/set/course/python")
for name, value in response.cookies.items():
print(name, "=", value)
Here, you loop through the cookie values returned by the server. You can also access a specific cookie via:
val = response.cookies.get("session_id")
Because response.cookies returns a RequestsCookieJar, you can use it like a dictionary for straightforward retrieval. However, unlike a standard dictionary, it tracks which domains and paths the cookies belong to, ensuring they are only sent back to the correct server.
Managing Session Cookies
To handle cookies across several requests, use the session object. For example:
import requests
session = requests.Session()
login_url = "https://httpbin.org/cookies/set/course/python"
credentials = {"username": "user", "password": "pass"}
login_resp = session.post(login_url, data=credentials)
protected_url = "https://httpbin.org/cookies"
resp2 = session.get(protected_url)
print(resp2.status_code, resp2.text)
In the flow above, your first request sets cookie values. The object stores these cookie values and then automatically sends cookies on the next request.
To persist cookies between script runs, you must explicitly save the cookie jar to a file using modules like pickle or by using http.cookiejar.MozillaCookieJar, as requests does not automatically save sessions to disk.
Conclusion
Cookies provide state in HTTP requests: authentication, sessions, and personalization depend on them. With import requests, you gain tools to send data manually, read it from a response object, and manage cookies across requests using a session object.
When you work with several requests and protected endpoints, leveraging cookies and sessions simplifies the flow and avoids repeated logins or lost states.