python requests module set headers

Python Requests Module Set Headers

When making a request to a website using Python, it is important to set headers in the request to provide information about the request being made. Headers can be used to provide information about the user agent, content type, encoding, and more.

The Python Requests module is a popular HTTP library for making requests in Python. It provides several methods to set headers in a request.

Method 1: Dictionary

The simplest way to set headers in a Python Requests module request is by passing a dictionary containing the headers to the headers parameter of the request method. For example:

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}

response = requests.get('https://www.example.com', headers=headers)

This sets the User-Agent header to the specified value in the request.

Method 2: add_header() Method

The add_header() method can be used to add headers to a Python Requests module request. For example:

import requests

headers = {'Content-type': 'application/json'}
url = 'https://www.example.com'
data = '{"key": "value"}'

req = requests.Request('POST', url, data=data)
req.add_header('Authorization', 'Bearer {}'.format(token))
response = requests.Session().send(req.prepare())

In this example, the add_header() method is used to add the Authorization header to the request.

Method 3: Session Object

The Session object can be used to set headers that will be applied to all subsequent requests made with that session. For example:

import requests

session = requests.Session()
session.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'})

response1 = session.get('https://www.example.com')
response2 = session.get('https://www.example.com/another-page')

In this example, the User-Agent header is set in the session object and will be applied to all subsequent requests made with that session.