A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/response-headers-python-requests/ below:

response.headers - Python requests - GeeksforGeeks

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 Output

Explanation:

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 header

Explanation:

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:

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:

Related Post: requests



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4