Handling Request Headers in Requests
HTTP headers provide additional context and metadata for requests and responses. In Python’s Requests library, you can customize headers to meet specific needs or work with default headers provided by the library.
Understanding Default Headers
The Requests library automatically includes some default headers in every request, such as User-Agent
. These headers can be viewed and overridden if necessary.
Example: Viewing Default Headers
import requests
response = requests.get("https://httpbin.org/headers")
print("Default Headers:", response.request.headers)
Output
Default Headers: {
'User-Agent': 'python-requests/2.25.1',
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'Connection': 'keep-alive'
}
Adding Custom Headers
You can specify custom headers for a request using the headers
parameter. This is useful for passing API keys, content types, or other required information.
Example: Adding Custom Headers
url = "https://api.example.com/data"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
print("Response Status Code:", response.status_code)
print("Response Headers:", response.headers)
Combining Default and Custom Headers
When you provide custom headers, they replace the corresponding default headers but do not overwrite all of them. If you need full control, ensure to include default headers explicitly.
Example: Customizing Default Headers
headers = {
"User-Agent": "MyCustomAgent/1.0",
"Accept": "application/json"
}
response = requests.get("https://httpbin.org/headers", headers=headers)
print("Headers Sent:", response.request.headers)
Output
Headers Sent: {
'User-Agent': 'MyCustomAgent/1.0',
'Accept': 'application/json',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive'
}
Try It Yourself
Problem 1: Custom User-Agent Header
Send a GET request to https://httpbin.org/headers
with a custom User-Agent
header (e.g., “MyApp/2.0”).
Show Solution
import requests
url = "https://httpbin.org/headers"
headers = {"User-Agent": "MyApp/2.0"}
response = requests.get(url, headers=headers)
print("Headers Sent:", response.request.headers)
print("Response JSON:", response.json())
Problem 2: Adding Authorization Header
Send a GET request to https://api.example.com/data
with an Authorization
header containing the value “Bearer YOUR_API_KEY”.
Show Solution
import requests
url = "https://api.example.com/data"
headers = {"Authorization": "Bearer YOUR_API_KEY"}
response = requests.get(url, headers=headers)
print("Status Code:", response.status_code)
print("Response Headers:", response.headers)
Customizing request headers is essential for working with APIs and handling various HTTP interactions. Experiment with default and custom headers to build flexible and robust applications!