Skip to content

Headers

The Headers class provides a case-insensitive dictionary-like interface for working with HTTP headers. It handles multi-value headers and preserves header order.

Passing headers to requests

Pass headers as a dictionary to the headers argument:

python
from zapros import AsyncClient

async with AsyncClient() as client:
    response = await client.request(
        "GET",
        "https://api.example.com/data",
        headers={
            "Authorization": "Bearer token123",
            "Accept": "application/json",
        },
    )
python
from zapros import Client

with Client() as client:
    response = client.request(
        "GET",
        "https://api.example.com/data",
        headers={
            "Authorization": "Bearer token123",
            "Accept": "application/json",
        },
    )

Case-insensitive access

Header names are case-insensitive — you can use any capitalization to retrieve values:

python
from zapros import Headers

headers = Headers({"Content-Type": "application/json"})

headers["content-type"]  # "application/json"
headers["CONTENT-TYPE"]  # "application/json"
headers["Content-Type"]  # "application/json"

Accessing response headers

Response objects have a headers attribute:

python
async with AsyncClient() as client:
    response = await client.request(
        "GET", "https://httpbin.org/get"
    )

    content_type = response.headers["content-type"]
    server = response.headers.get("server")

    if "set-cookie" in response.headers:
        cookies = response.headers.getall("set-cookie")
python
with Client() as client:
    response = client.request(
        "GET", "https://httpbin.org/get"
    )

    content_type = response.headers["content-type"]
    server = response.headers.get("server")

    if "set-cookie" in response.headers:
        cookies = response.headers.getall("set-cookie")

Multi-value headers

Some headers can appear multiple times in HTTP messages (like Set-Cookie). Use getall() to retrieve all values:

python
from zapros import Headers

headers = Headers([
    ("set-cookie", "session=abc123"),
    ("set-cookie", "user=john"),
])

headers[
    "set-cookie"
]  # "session=abc123" (returns first value)
headers.getall(
    "set-cookie"
)  # ["session=abc123", "user=john"]

Creating and modifying headers

Creating a Headers object

You can create a Headers object from a dictionary or a list of tuples:

python
from zapros import Headers

headers = Headers({"Content-Type": "application/json"})

headers = Headers([
    (
        "content-type",
        "application/json",
    ),
    ("authorization", "Bearer token"),
])

Adding headers

Use add() to append a header value without replacing existing ones:

python
headers = Headers({"accept": "application/json"})

headers.add("accept", "text/html")

headers.getall(
    "accept"
)  # ["application/json", "text/html"]

Extending headers

Use extend() to add multiple headers at once:

python
headers = Headers({"user-agent": "zapros"})

headers.extend({
    "authorization": "Bearer token",
    "accept": "application/json",
})

headers.extend([
    ("x-custom-header", "value1"),
    ("x-custom-header", "value2"),
])

Dictionary operations

The Headers class supports standard dictionary operations:

python
from zapros import Headers

headers = Headers({
    "content-type": "application/json",
    "authorization": "Bearer token",
})

"content-type" in headers  # True
"x-custom" in headers  # False

len(headers)  # 2

for name in headers:
    print(name)  # Iterates over header names

for name, value in headers.items():
    print(f"{name}: {value}")

list(headers.keys())  # ["content-type", "authorization"]
list(
    headers.values()
)  # ["application/json", "Bearer token"]

Copying headers

Use copy() to create a shallow copy:

python
original = Headers({"content-type": "application/json"})
copied = original.copy()

copied.add("authorization", "Bearer token")

len(original)  # 1 (unchanged)
len(copied)  # 2

Converting to a list

Use list() to get all headers as tuples:

python
headers = Headers({
    "content-type": "application/json",
    "authorization": "Bearer token",
})

headers.list()
# [("content-type", "application/json"), ("authorization", "Bearer token")]

This is useful when you need to pass headers to other libraries or serialize them.

Automatic headers

Zapros automatically adds certain headers if they're not already present:

  • Host — derived from the URL
  • Accept — set to */* by default
  • User-Agent — identifies the Zapros client
  • Accept-Encoding — advertises supported compression (gzip, deflate, br)
  • Content-Length — calculated for byte bodies
  • Transfer-Encoding: chunked — set for streaming bodies

You can override any of these by explicitly passing them in your request.