Advanced HTTP Requests in Python with httpx: Client Objects and HTTP/2 Support
Basic GET request with custom headers:
import httpx
# Define custom headers to mimic a browser
custom_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Firefox/88.0'
}
# Make a GET request with custom headers
api_response = httpx.get('https://httpbin.org/get', headers=custom_headers)
# Print the response content
print(api_response.text)
By default, httpx uses HTTP/1.1. To enable HTTP/2.0 support, you need to explicitly configure it:
import httpx
# Create a client with HTTP/2.0 enabled
http2_client = httpx.Client(http2=True)
# Make a request to an HTTP/2.0 enabled endpoint
response = http2_client.get('https://spa16.scrape.center/')
print(response.text)
Client Objects
The httpx library provides a Client object that can be compared to the Session object in the requests library:
import httpx
# Using context manager for automatic resource cleanup
with httpx.Client() as client:
response = client.get('https://httpbin.org/get')
print(response)
This usage is equivalent to the following manual resource management:
import httpx
# Manual resource management
client = httpx.Client()
try:
response = client.get('https://httpbin.org/get')
finally:
client.close()
Both approaches achieve the same result, with the only difference being the need to manually call the client's close method in the second approach.
When using the Client object, you can specify various parameters such as headers:
import httpx
# Define target URL and headers
target_url = 'http://httpbin.org/headers'
request_headers = {'User-Agent': 'my-application/1.0.0'}
# Create client with headers
with httpx.Client(headers=request_headers) as client:
api_response = client.get(target_url)
# Extract and print the User-Agent from response
print(api_response.()['headers']['User-Agent'])
For more advanced Client usage, refer to the official documentation: https://www.python-httpx.org/advanced/
Using Client with HTTP/2.0
import httpx
# Create HTTP/2.0 enabled client
http2_client = httpx.Client(http2=True)
# Make request and check HTTP version
response = http2_client.get('https://httpbin.org/get')
print(response.text)
print(response.http_version)
AsyncClient for Asynchronous Requests
The httpx library also supports asynchronous requests through the AsyncClient:
import httpx
import asyncio
async def fetch_data():
# Create async client
async with httpx.AsyncClient() as client:
# Make asynchronous request
response = await client.get('https://httpbin.org/get')
return response.text
# Run the async function
asyncio.run(fetch_data())