response.headers - Python requests
Last Updated : 12 Jul, 2025
The response.headers object in Python's requests library functions as a special dictionary that contains extra information provided by the server when we make an HTTP request. It stores metadata like content type, server details and other headers, such as cookies or authorization tokens. The keys in response.headers are case-insensitive, meaning we can access them in any case. Example:
In this example, we use GitHub's API to retrieve the headers of a response.
Python import requests r = requests.get('https://api.github.com/') # accessing response headers h = r.headers print(h) # accessing a specific header print(h['Content-Type'])
Output
GitHub API Headers OutputExplanation:
- requests.get() function sends an HTTP GET request to the given URL.
- r.headers gives us the headers of the response, which is stored as a dictionary-like object.
- We can then extract a specific header (like Content-Type) using standard dictionary access.
Here are some of the most common HTTP response headers we might encounter:
Header Name | Description | Example Value |
---|
Content-Type | Type of content returned (HTML, JSON, etc.) | application/json; charset=utf-8 |
---|
Content-Length | Size of the response body in bytes | 348 |
---|
Date | Date and time when the response was generated | Wed, 08 Apr 2025 12:00:00 GMT |
---|
Server | Info about the web server | nginx/1.18.0 |
---|
Set-Cookie | Cookie to be stored on client | sessionId=abc123; Path=/; HttpOnly |
---|
Cache-Control | Instructions for caching | no-cache, no-store, must-revalidate |
---|
Expires | When the content should be considered outdated | Thu, 01 Jan 1970 00:00:00 GMT |
---|
Location | Redirect location | https://example.com/login |
---|
Authorization | Info about required authentication (often in requests, too) | Bearer token123 |
---|
X-RateLimit-Limit | Max number of requests allowed | 60 |
---|
Example 1: In this example, we demonstrate how to detect HTTP redirection (status codes 301 or 302) and retrieve the Location header, which indicates where the client should be redirected.
Python import requests # Send GET request without auto-redirect r = requests.get('http://www.google.com/', allow_redirects=False) # Check for redirection (301 or 302) if r.status_code in {301, 302}: print(r.headers['Location']) else: print("No redirection.")
Output
Location headerExplanation:
- allow_redirects=False parameter ensures that the request won't automatically follow redirects.
- If the status code indicates a redirection (301 or 302), the Location header will contain the new URL to which the client should be redirected.
Example 2: In this example, we fetch JSON data from an API and check the Content-Type to ensure we handle the response appropriately.
Python import requests # Fetch data and parse if JSON r = requests.get('https://jsonplaceholder.typicode.com/posts') if 'application/json' in r.headers['Content-Type']: print(r.json()) else: print("Not JSON.")
Output
[{'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et
suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est
autem sunt rem eveniet architecto'}, {'userId': 1, 'id': 2, 'title': 'qui est esse', 'body': 'est rerum tempore vitae\nsequi sint nihil
reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam
non debitis possimus qui neque nisi nulla'},.....................................................
Explanation:
- requests.get() function is used to fetch data from an API.
- We then check the Content-Type header to ensure that the response is in JSON format before parsing it with r.json(). If it's not JSON, we print a message indicating that.
Example 3: Extracting Cookies from Response Headers
In this example, we send a request and check the Set-Cookie header to see what cookies the server sends back.
Python import requests # Send a GET request to the website r = requests.get('https://httpbin.org/cookies') # Check for Set-Cookie header if 'Set-Cookie' in r.headers: print("Cookie from server:", r.headers['Set-Cookie']) else: print("No cookie set.")
Output
Cookie from server: mycookie=value; Path=/
Explanation:
- The URL https://httpbin.org/cookies sets a cookie named mycookie.
- r.headers['Set-Cookie'] allows us to access the cookie sent by the server.
- This can be useful when we need to manually manage or inspect cookies during web scraping or session handling.
Related Post: requests
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice
My Profile