python requests headers example

Python Requests Headers Example

If you're working with APIs or scraping websites using Python's Requests library, you might need to specify custom headers in your HTTP requests. Headers are used to provide additional information about the request, such as the user agent or authentication tokens.

Setting Headers in Requests

To set headers in a Requests HTTP request, you can create a dictionary of key-value pairs for the headers you want to include, and pass it as the headers parameter when making the request:


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)

In this example, we're setting the User-Agent header to mimic a Google Chrome browser on Windows 10. This can be useful when some websites block requests from certain user agents, or when you want to access mobile versions of websites.

Inspecting Headers in Responses

If you want to inspect the headers of a response from a request, you can access the headers attribute of the response object:


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

This will print out a dictionary of headers sent by the server in the response. Some common headers include Content-Type, Content-Length, and Set-Cookie.

Multiple Ways to Set Headers

There are multiple ways to set headers in Requests, depending on your use case:

  • Passing a dictionary of headers to the headers parameter of the request
  • Passing a list of (key, value) tuples to the headers parameter of the request
  • Using the headers attribute of the Session object to set headers that will be used in all requests made with that session

Here's an example of using a list of tuples to set headers:


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'),
    ('X-API-Key', 'secret-key'),
]

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

And here's an example of setting headers on a Session object:


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/page1')
response2 = session.get('https://www.example.com/page2')

In this example, the User-Agent header will be sent with both requests made with the same session object.