Last modified: Jan 29, 2025 By Alexander Williams
Python httpx.request() Guide: HTTP Requests Made Simple
Python's httpx.request()
is a powerful method for making HTTP requests. It is part of the httpx library, which is designed for modern web development. This guide will help you understand how to use it effectively.
Table Of Contents
What is httpx.request()?
The httpx.request()
method allows you to send HTTP requests. It supports various HTTP methods like GET, POST, PUT, and DELETE. This flexibility makes it a great choice for web scraping, APIs, and more.
Installing httpx
Before using httpx.request()
, you need to install the httpx library. You can do this using pip. Check out our step-by-step guide for detailed instructions.
pip install httpx
Basic Syntax of httpx.request()
The basic syntax of httpx.request()
is straightforward. You need to specify the HTTP method and the URL. Here's an example:
import httpx
response = httpx.request('GET', 'https://example.com')
print(response.status_code)
200
In this example, we send a GET request to https://example.com. The response object contains the status code, which is printed out.
Using httpx.request() with Different HTTP Methods
You can use httpx.request()
with various HTTP methods. Here are some examples:
GET Request
A GET request retrieves data from a server. Here's how you can do it:
response = httpx.request('GET', 'https://jsonplaceholder.typicode.com/posts/1')
print(response.json())
{'userId': 1, 'id': 1, 'title': '...', 'body': '...'}
POST Request
A POST request sends data to a server. Here's an example:
data = {'title': 'foo', 'body': 'bar', 'userId': 1}
response = httpx.request('POST', 'https://jsonplaceholder.typicode.com/posts', json=data)
print(response.json())
{'id': 101, 'title': 'foo', 'body': 'bar', 'userId': 1}
For more details on POST requests, check out our Python httpx.post() Guide.
PUT Request
A PUT request updates existing data on a server. Here's how you can use it:
data = {'id': 1, 'title': 'foo', 'body': 'bar', 'userId': 1}
response = httpx.request('PUT', 'https://jsonplaceholder.typicode.com/posts/1', json=data)
print(response.json())
{'id': 1, 'title': 'foo', 'body': 'bar', 'userId': 1}
For more on PUT requests, visit our Python httpx.put() Guide.
Handling Errors
When making HTTP requests, errors can occur. The httpx.request()
method raises exceptions for network issues or invalid responses. Here's how to handle them:
try:
response = httpx.request('GET', 'https://example.com/nonexistent')
response.raise_for_status()
except httpx.HTTPStatusError as e:
print(f"HTTP error occurred: {e}")
HTTP error occurred: 404 Client Error: Not Found for url: https://example.com/nonexistent
Conclusion
The httpx.request()
method is a versatile tool for making HTTP requests in Python. It supports multiple HTTP methods and provides robust error handling. Whether you're working with APIs or web scraping, httpx is a great choice.
For more advanced usage, explore our guides on httpx.options() and httpx.delete().