How to set Cookie in Python HTTP request?

Python Tutorials

Summary: In this tutorial, you will learn with examples to set cookie to HTTP requests in Python.

Adding a cookie to a HTTP request ensures that the future requests contains all sort of information required to maintain a proper session.

Using cookies in HTTP request enables the web server to remember preferences (such as login, language, etc.) over a period of time.

In Python, you can easily set cookie to HTTP request using the requests or urllib python module. Let’s see an example of each.

Use request.session() to Set Cookie

The request.session() object is similar to requests object for making HTTP request. Additionally, it has inbuilt mechanism to persist certain parameters such as cookies across multiple requests.

import requests

s = requests.Session()

s.get('https://pencilprogrammer.com')
r = s.get('https://pencilprogrammer.com/privacy-policy/')

print(r.cookies)

This code is similar to the following code:

import requests

r1 = requests.get('https://pencilprogrammer.com')
r2 = requests.get('https://pencilprogrammer.com/privacy-policy/',cookies=r1.cookies)

print(r2.cookies)
# '{"cookies": {"sessioncookie": "123456789"}}'

Also, if you are making several requests to the same host using the first method, the underlying TCP connection will be reused, making your request comparatively faster.

How to create a new Cookie and add to a HTTP requests?

In previous example, the subsequent request calls were using the cookie received from the former request (set by the server).

But the requests module also allows you to manually set key and value pairs for the cookie and send it with the request call.

All you need to do, is to define the cookie as a dictionary and pass it to the cookies parameter of the get or post call as follows:

import requests

my_cookies = {"cookiename": "cookievalue"}

response requests.get("https://pencilprogrammer.com", cookies=my_cookies)

print(response.text)

Use urllib.request.add_header() to Set Cookie

Another way to manually set cookie is by using the urllib module.

import urllib

a_request = urllib.request.Request("https://pencilprogrammer.com")

a_request.add_header("Cookie", "cookiename=cookievalue")

print(a_request.header_items())

In this method, we called the urllib.request.Request(url) method to get a Request object from the specified url.

Then we call urllib.Request.add_header(name, content) method to explicitly add a cookie named name containing the key-value pair content to the previous result urllib.Request.

This method is useful when you want to manually specify the name and value pairs for a cookie.

These were the ways using which you can easily add cookie to HTTP request in Python programming language.

Leave a Reply

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