Of course! Working with HTTP headers is a fundamental task in Python, whether you're making requests to other APIs or building your own web server.
Here’s a comprehensive guide covering how to work with HTTP headers in Python using the two most common scenarios:
- Making HTTP Requests (Client-side): Sending headers with your requests and reading headers from the response.
- Building a Web Server (Server-side): Reading headers from incoming requests and setting headers in your responses.
Making HTTP Requests (Client-side)
The most popular library for this is the requests library. It's powerful, user-friendly, and the de facto standard for HTTP in Python.
Installation
First, if you don't have it installed, open your terminal or command prompt and run:
pip install requests
A. Sending Custom Headers in a Request
You often need to send headers to provide information to the server, such as your User-Agent, Accept content types, or authentication tokens (like an API key).
You pass headers as a dictionary to the headers parameter of any requests method (get, post, etc.).
Example: Sending a User-Agent and an API Key
import requests
# Define the headers you want to send
# Keys are header names (strings), values are header values (strings)
headers_to_send = {
'User-Agent': 'MyCoolApp/1.0 (myemail@example.com)',
'Accept': 'application/json', # Tell the server we expect JSON back
'X-API-Key': 'my-secret-api-key-12345' # A custom header for authentication
}
url = 'https://httpbin.org/get' # A great testing service
# Make the GET request with the custom headers
response = requests.get(url, headers=headers_to_send)
# Check if the request was successful
response.raise_for_status() # Raises an exception for bad status codes (4xx or 5xx)
print(f"Status Code: {response.status_code}")
print("\n--- Request Headers Sent (as seen by httpbin.org) ---")
# The response from httpbin.org includes the headers it received
print(response.json()['headers'])
Output:
Status Code: 200
--- Request Headers Sent (as seen by httpbin.org) ---
{
'Accept': 'application/json',
'X-Api-Key': 'my-secret-api-key-12345',
'User-Agent': 'MyCoolApp/1.0 (myemail@example.com)',
'Host': 'httpbin.org',
'X-Amzn-Trace-Id': 'Root=...'}
}
Notice how httpbin.org correctly received our custom headers.
B. Reading Headers from a Response
When you get a response from a server, it comes with its own set of headers. You can access them easily.
Example: Reading Response Headers
import requests
url = 'https://github.com'
response = requests.get(url)
# Access headers from the response object
# response.headers is a special dictionary-like object
print("--- All Response Headers ---")
print(response.headers)
# You can access a specific header by its name (case-insensitive)
# If the header doesn't exist, it returns None by default
content_type = response.headers.get('Content-Type')
server = response.headers.get('Server')
print(f"\nContent-Type: {content_type}")
print(f"Server: {server}")
# You can also access headers using the .get() method for safety
# This prevents errors if a header is missing
x_frame_options = response.headers.get('X-Frame-Options')
print(f"X-Frame-Options: {x_frame_options}")
Key points about response.headers:
- It's a case-insensitive dictionary.
response.headers['Content-Type']is the same asresponse.headers['content-type']. - It returns
Noneif you use.get()and the header is not found, which is safer than causing aKeyError.
Building a Web Server (Server-side)
When you build a web server (e.g., using Flask or Django), you need to read headers from the incoming request and set headers in your outgoing response.
We'll use Flask for this example as it's lightweight and great for demonstration.
Installation
pip install Flask
A. Reading Headers from an Incoming Request
In Flask, you can access request headers through the request object.
Example Flask App (app.py)
from flask import Flask, request, Response
app = Flask(__name__)
@app.route('/check-headers')
def check_headers():
# The request.headers object is a dictionary-like object
# Accessing headers is case-insensitive
user_agent = request.headers.get('User-Agent')
accept_language = request.headers.get('Accept-Language')
# You can also iterate through all headers
all_headers = {}
for header, value in request.headers:
all_headers[header] = value
response_text = f"Your User-Agent is: {user_agent}\n"
response_text += f"Your preferred language is: {accept_language}\n\n"
response_text += "Here are all the headers I received:\n"
response_text += str(all_headers)
return response_text
if __name__ == '__main__':
app.run(debug=True)
How to run and test:
- Save the code as
app.py. - Run
python app.pyin your terminal. - Open your web browser and go to
http://127.0.0.1:5000/check-headers. - You will see the headers your browser sent to the Flask server.
B. Setting Headers in an Outgoing Response
You can easily add custom headers to your response using the Response object or Flask's make_response helper.
Example Flask App with Custom Response Headers
from flask import Flask, request, Response
app = Flask(__name__)
@app.route('/api-data')
def get_api_data():
# The data you want to send
data = {"message": "This is a JSON response from my API"}
# Create a response object
response = Response(
response=data, # The body of the response
status=200, # The HTTP status code
mimetype='application/json' # The content type
)
# Add custom headers
response.headers['X-Custom-Header'] = 'This is a custom value'
response.headers['X-API-Version'] = '1.0'
return response
if __name__ == '__main__':
app.run(debug=True)
How to test:
- Run the Flask app.
- Open your browser or use
curlto accesshttp://127.0.0.1:5000/api-data.
Using curl:
curl -I http://127.0.0.1:5000/api-data
The -I flag fetches only the headers. You'll see your custom headers in the output:
HTTP/1.1 200 OK
X-Custom-Header: This is a custom value
X-API-Version: 1.0
Content-Type: application/json
...
Summary
| Task | Library | Key Code Snippet | Description |
|---|---|---|---|
| Send Request Headers | requests |
requests.get(url, headers={'Key': 'Value'}) |
Pass a dictionary of headers to the headers parameter. |
| Read Response Headers | requests |
response.headers.get('Content-Type') |
Access headers from the response.headers object (case-insensitive). |
| Read Request Headers | Flask |
request.headers.get('User-Agent') |
Access headers from the request.headers object in a Flask route. |
| Set Response Headers | Flask |
response.headers['X-Custom'] = 'Value' |
Add or modify headers on a Flask Response object before returning it. |
