Sending Python Requests with HTTP Headers

You can pass HTTP headers to Python Requests Library methods using the headers = parameter. Headers are passed as a dictionary of header name: header value pairs. The Requests Library does not change its behavior depending on the passed headers but simply redirects them to the server. Header names must be ANSI strings, and header values can be strings, bytestring, or Unicode. The response.headers object contains the HTTP headers received from the server. In this Python Requests Headers Example, we send custom HTTP headers to the ReqBin echo URL. Click Execute to run the Python Requests Headers Example online and see the result.
Sending Python Requests with HTTP Headers Execute
import requests

url = 'https://reqbin.com/echo'
headers = {'Accept': '*/*', 'X-User-IP': '1.1.1.1'}

r = requests.get(url, headers=headers)

for key, val in r.headers.items():
    print(key, ':', val)
Updated: Viewed: 18643 times

What is the Python Request Library?

Requests Library is a popular Library that makes it easy to send HTTP requests using POST, GET and DELETE methods, upload files, post JSON and XML data, and submit HTML forms. The Requests Library automatically validates server SSL certificates and supports International Domain Names and session cookies. The Requests Library is based on the urllib3 library and hides the complexity of making HTTP requests behind a simple API. Although the Requests Library is not included in the Python distribution, almost everyone uses Requests Library because the Python code for working with HTTP becomes short, simple, and straightforward.

What are HTTP Headers?

HTTP headers allow clients to send additional information to the server and enable the server to provide additional information to clients, such as the data type and size in the POST content. HTTP headers are invisible to the end-user and are only visible to clients, servers, and network administrators. Custom HTTP headers are used to provide additional information related to the current request or response for troubleshooting purposes. HTTP headers include a case-insensitive name followed by a colon ":" and its value. Spaces before the value are ignored.

An example of HTTP headers when sending a POST request to the server:

HTTP Headers Example
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 81
      
[post data]

How to install the Python Requests Library?

You can install the Requests Library using the pip package installer for Python.

Install Python Requests Library
pip install requests

After installing the Requests Library, you can use it in your code by importing the Requests Library with the following Python code:

Import Requests Syntax
import requests

Passing HTTP Headers to Requests Library methods

You can pass HTTP headers to the Requests library methods as the second argument to the requests.get(), post(), put(), patch(), and delete() methods:

Python Requests with Headers Syntax
requests.get(URL, headers=[headers])
requests.post(URL, headers=[headers])
requests.delete(URL, headers=[headers])

Where:
  • URL: target URL endpoint.
  • headers: a list of HTTP Headers to send to the server.

Request Library methods do not change their behavior regardless of the custom headers you send. Custom headers are passed in the final request and have lower priority than standard headers:

Example of sending additional HTTP Headers in Python

An example of sending a request with custom HTTP headers to the ReqBin echo URL:

Python Request with Custom HTTP Headers Example
import requests

url = 'https://reqbin.com/echo'
headers = {'User-Agent': 'ReqBin Python Client/1.0'}
      
r = requests.get(url, headers=headers)
print(r.text)

How to read response headers using Python requests library?

The Python Requests Library stores the server's response HTTP headers as a Python dictionary in the response.headers object. You can work with headers object as you would with a regular Python dictionary. For example, you can access a header by name or check for the existence of a header using the in operator:

Python Server Response Headers Example
import requests

r = requests.get('https://reqbin.com/echo')
      
# print all headers
print(r.headers)
      
# access a header by name
print(r.headers['Content-Type'])
      
# check existance of a header in server response headers
print('Content-Type' in r.headers)

The server can send the same header multiple times with different values. The Request Library combines such headers into a single comma-separated header, per RFC 7230.

See also