Last modified: Jan 30, 2025 By Alexander Williams

Python httpx.cookies() Guide: HTTP Cookies

HTTP cookies are essential for managing session data in web requests. In Python, the httpx library provides a powerful way to handle cookies. This guide will walk you through using httpx.cookies() effectively.

What Are HTTP Cookies?

Cookies are small pieces of data stored on the client side. They are sent with every request to the server. Cookies help maintain stateful sessions, track user activity, and personalize user experiences.

In httpx, cookies are managed using the Cookies class. This class allows you to set, get, and manipulate cookies easily.

Using httpx.cookies()

The httpx.cookies() method is used to create a Cookies object. This object can be used to manage cookies in your HTTP requests. Below is an example of how to use it.


import httpx

# Create a Cookies object
cookies = httpx.Cookies()

# Set a cookie
cookies.set("session_id", "12345")

# Make a request with cookies
response = httpx.get("https://example.com", cookies=cookies)

print(response.cookies)

In this example, we create a Cookies object and set a cookie named session_id. The cookie is then sent with the request to https://example.com.

Getting Cookies from a Response

When you receive a response from a server, you can access the cookies using the cookies attribute. Here's how you can do it.


import httpx

# Make a request
response = httpx.get("https://example.com")

# Get cookies from the response
cookies = response.cookies

print(cookies)

This code retrieves cookies from the server's response. You can then use these cookies in subsequent requests.

Manipulating Cookies

The Cookies class provides several methods to manipulate cookies. You can add, update, or delete cookies as needed. Here's an example.


import httpx

# Create a Cookies object
cookies = httpx.Cookies()

# Add a cookie
cookies.set("user_id", "67890")

# Update a cookie
cookies.set("user_id", "54321")

# Delete a cookie
cookies.delete("user_id")

print(cookies)

In this example, we add, update, and delete a cookie named user_id. The delete method removes the cookie from the Cookies object.

Using Cookies with httpx.Client()

When using httpx.Client(), you can manage cookies across multiple requests. This is useful for maintaining session data. Here's an example.


import httpx

# Create a client with cookies
client = httpx.Client(cookies={"session_id": "12345"})

# Make a request
response = client.get("https://example.com")

print(response.cookies)

This code creates a client with a predefined cookie. The cookie is sent with every request made using this client. For more details, check out our Python httpx.Client() Guide.

Handling Cookie Expiry

Cookies often have an expiry date. You can check the expiry date of a cookie using the expires attribute. Here's how.


import httpx

# Create a Cookies object
cookies = httpx.Cookies()

# Set a cookie with an expiry date
cookies.set("session_id", "12345", expires=3600)

# Check the expiry date
print(cookies.get("session_id").expires)

In this example, we set a cookie with an expiry time of 3600 seconds. The expires attribute is then used to check the expiry date.

Conclusion

Managing cookies is a crucial part of web development. The httpx.cookies() method in Python makes it easy to handle cookies in your HTTP requests. Whether you're setting, getting, or manipulating cookies, httpx provides a simple and efficient way to do it.

For more advanced topics, consider exploring our guides on Python httpx.Request() and Python httpx.Response().