Python Requests with Headers
If you are working on a Python project that requires sending HTTP requests, you'll likely be using the requests
module. One important aspect of sending requests is setting headers, which provide additional information about the request being sent.
Setting Headers with Requests
To set headers with requests, you can use the headers
parameter when making your request. The headers
parameter is a dictionary containing the header keys and values.
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',
'Content-Type': 'application/json'
}
response = requests.get('https://example.com', headers=headers)
print(response.status_code)
In this example, we are setting several headers including the user agent, accept language, accept encoding, connection, and content type. We then make a GET request to https://example.com with these headers and print out the status code of the response.
Multiple Ways to Set Headers
There are multiple ways to set headers with requests, including:
- Using the
headers
parameter as shown above - Using the
.header
method on a request object - Setting headers for all requests by modifying the session object
The method you choose will depend on your specific use case.
Using the .header Method
The .header
method allows you to set or modify headers on a request object. Here's an example:
import requests
response = requests.get('https://example.com')
response.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
response.headers['Accept-Language'] = 'en-US,en;q=0.5'
response.headers['Accept-Encoding'] = 'gzip, deflate, br'
response.headers['Connection'] = 'keep-alive'
response.headers['Content-Type'] = 'application/json'
print(response.status_code)
In this example, we first make a GET request to https://example.com without setting any headers. We then use the .header
method to set the headers on the response object. Finally, we print out the status code of the response.
Modifying the Session Object
If you need to set headers for all requests made with the requests
module, you can modify the session object. Here's an example:
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',
'Content-Type': 'application/json'
}
session = requests.Session()
session.headers.update(headers)
response = session.get('https://example.com')
print(response.status_code)
In this example, we set the headers we want to use as a dictionary. We then create a session object and update its headers with the dictionary. Finally, we make a GET request to https://example.com using the session object and print out the status code of the response.