How to Set and Inspect HTTP Request Headers with Python requests
This guide explains why request headers are needed, shows how to set them using Python's requests library—including basic headers and authentication tokens—and demonstrates how to view both request and response headers along with common header usage tips.
1. Why set request headers? Request headers convey additional information to the server, such as User-Agent, Content-Type, Accept, and Authorization.
User-Agent: identifies the client type (e.g., browser or app)
Content-Type: specifies the request body format (e.g., application/json)
Accept: tells the server the response types the client expects
Authorization: used for authentication, such as a Bearer token2. How to set request headers?
2.1 Basic settings Use the headers dictionary parameter in requests calls.
import requests
# Define request headers
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36',
'Accept': 'application/json',
'Content-Type': 'application/json'
}
# Send GET request
response = requests.get('http://example.com', headers=headers)
print(response.status_code)
print(response.text)In this example, User-Agent, Accept, and Content-Type are set.
2.2 Setting authentication information Include an Authorization header when the API requires a token.
headers = {
'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
'Accept': 'application/json'
}
response = requests.get('https://api.example.com/data', headers=headers)
print(response.json())3. Viewing request and response headers
3.1 View request headers After sending a request, inspect the actual headers with response.request.headers: print(response.request.headers) 3.2 View response headers The response headers contain server information such as Content-Type and Set-Cookie: print(response.headers) 4. Common request headers and their purposes
User-Agent: simulates different browsers or clients
Content-Type: specifies the request body format (e.g., application/json)
Accept: specifies the response format the client expects
Authorization: used for authentication, usually containing a token
Cookie: used for session management5. Precautions
Header values must be strings, bytes, or Unicode.
Header names are case‑insensitive.
The requests library adds default headers (e.g., Accept-Encoding) which can be overridden by custom headers.
6. Summary By setting request headers you can simulate various client scenarios for API testing; with requests, simply pass a dictionary to the headers argument to customize them.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
