Lesson 6 of 12 · Data on the Wire
Headers
Learn what HTTP headers are, the request and response headers you will use with APIs, such as Content-Type, Accept and Authorization, and how to read and send headers in Python.
- 6 min read
- Beginner
- Reviewed
- What request headers and response headers are
- The headers you will meet most often with APIs
- Why header names ignore upper and lower case
- How to send and read headers in Python
Information about the message
Think of posting a parcel. The parcel's contents are the body. The label on the outside says who it is for, what is inside and how to handle it. Headers are that label.
The headers you will meet most
| Header | Sent by | Example | Purpose |
|---|---|---|---|
Content-Type | both | application/json | the format of the body |
Accept | client | application/json | the format you want back |
Authorization | client | Bearer abc123 | proof of who you are |
User-Agent | client | python-requests/2.32 | which program is calling |
Content-Length | both | 348 | the size of the body in bytes |
Retry-After | server | 30 | how many seconds to wait before trying again |
Header names ignore case
Content-Type, content-type and CONTENT-TYPE are the same header. Different servers write them differently, so a careful program compares names in lower case.
raw_headers = """Content-Type: application/json; charset=utf-8
Date: Tue, 17 Nov 2026 11:05:00 GMT
X-RateLimit-Limit: 600
X-RateLimit-Remaining: 598"""
headers = {}
for line in raw_headers.split("\n"):
name, value = line.split(": ", 1)
headers[name.lower()] = value # store every name in lower case
print(headers["content-type"])
print("Requests left:", int(headers["x-ratelimit-remaining"]))
def get_header(headers, name):
return headers.get(name.lower())
print(get_header(headers, "CONTENT-TYPE"))
print(get_header(headers, "Retry-After"))Notice two details. A header value is always text, so convert numbers with int(). And a value can carry extra details after a semicolon, such as charset=utf-8, the character encoding of the text.
Sending headers from Python
With requests, headers are a dictionary passed as headers=. httpbin.org is a free testing service whose /headers endpoint simply sends back the headers it received, so you can see exactly what went out:
import requests
my_headers = {
"Accept": "application/json",
"X-Course": "apis-and-http",
}
response = requests.get("https://httpbin.org/headers", headers=my_headers, timeout=10)
received = response.json()["headers"]
print("Accept: ", received.get("Accept"))
print("X-Course:", received.get("X-Course"))Reading response headers
response.headers behaves like a dictionary that ignores case, so you do not need to lower-case anything yourself:
import requests
response = requests.get(
"https://api.open-meteo.com/v1/forecast",
params={"latitude": 17.38, "longitude": 78.49, "current": "temperature_2m"},
timeout=10,
)
print("Content-Type:", response.headers.get("content-type"))
print("Limit: ", response.headers.get("X-RateLimit-Limit"))
print("Remaining: ", response.headers.get("x-ratelimit-remaining"))Open-Meteo uses custom headers to tell you how many requests you may make per minute and how many you have left. You will use headers like these in the Rate Limits lesson.
Common mistakes
Read the headers of a response that says you have run out of requests.
raw = """Content-Type: application/json Retry-After: 30 X-RateLimit-Limit: 100 x-ratelimit-remaining: 0""" # 1. Turn raw into a dictionary called headers, with every NAME in lower case. # 2. Print the content type. # 3. If the remaining requests (as a number) is 0, print: # Limit reached, wait <retry-after> seconds # Otherwise print: OK to continue # Output must be exactly: # Content type: application/json # Limit reached, wait 30 seconds
Which header tells the receiver what format the body is in?
Summary
- Headers are
Name: valuelines of extra information. The body carries the data. - Request headers come from the client, response headers from the server.
- Key headers:
Content-Type,Accept,Authorization,Retry-Afterand custom headers such asX-RateLimit-Remaining. - Header names ignore case, and header values are always text.
- In
requests, send headers withheaders=and read them fromresponse.headers.
Next: the two ways to send details with a request, query parameters and the body.
Finished reading? Tick it off to track your progress.