Velamuvi AIAcademy

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
In this lesson you will learn
  • 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

HeaderSent byExamplePurpose
Content-Typebothapplication/jsonthe format of the body
Acceptclientapplication/jsonthe format you want back
AuthorizationclientBearer abc123proof of who you are
User-Agentclientpython-requests/2.32which program is calling
Content-Lengthboth348the size of the body in bytes
Retry-Afterserver30how 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.

Try it yourselfPython
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:

Try it yourselfPython
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:

Try it yourselfPython
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

Practice exercise

Read the headers of a response that says you have run out of requests.

Your solutionPython
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
Knowledge check

Which header tells the receiver what format the body is in?

Summary

  • Headers are Name: value lines 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-After and custom headers such as X-RateLimit-Remaining.
  • Header names ignore case, and header values are always text.
  • In requests, send headers with headers= and read them from response.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.