Python httpx: How to set headers

Updated: January 6, 2024 By: Guest Contributor Post a comment

Introduction

Setting headers in HTTP requests is essential for accessing APIs that require authentication, passing custom data, and directing how servers should process requests. This guide explores how to set headers in Python using the httpx library.

Getting Started

To begin, install httpx if you haven’t already:

pip install httpx

After installation, you can start making HTTP requests. Here’s a simple GET request:

import httpx

response = httpx.get('https://example.com')
print(response.status_code)

Setting Headers

To pass headers, use the headers argument:

headers = {'User-Agent': 'my-app/0.0.1'}
response = httpx.get('https://api.slingacademy.com', headers=headers)

This sends a custom User-Agent.

Dynamic Headers

You might want to set headers on a per-request basis dynamically:

def get_with_auth(url, token):
    headers = {'Authorization': f'Bearer {token}'}
    return httpx.get(url, headers=headers)

Advanced: Sessions

For multiple requests, use a Client instance to persist headers:

with httpx.Client(headers={'User-Agent': 'my-app/0.0.1'}) as client:
    response = client.get('https://api.slingacademy.com')

Where Client reuses the headers for each request.

Custom Authentication Classes

Create a custom authentication class for setting headers if needed:

class BearerAuth(httpx.Auth):
    def __init__(self, token):
        self.token = token

    def auth_flow(self, request):
        request.headers['Authorization'] = f'Bearer {self.token}'
        yield request

auth = BearerAuth('YOUR_TOKEN')
response = httpx.get('https://example.com', auth=auth)

Error Handling

Always handle potential errors accordingly:

try:
    response = httpx.get('https://example.com', headers=headers)
    response.raise_for_status()
except httpx.HTTPStatusError as exc:
    print(f'Error response {exc.response.status_code} while requesting {exc.request.url!r}.')

This ensures graceful handling of unsuccessful responses.

Conclusion

Setting headers with httpx is a powerful feature that can control how your requests are received and processed by web servers, and it is vital for tasks like API communication. Throughout your development with httpx, remember to handle exceptions properly to maintain robust and resilient applications.