Last modified: Jan 29, 2025 By Alexander Williams
Python httpx.delete() Guide: HTTP DELETE Requests
Python's httpx
library is a powerful tool for making HTTP requests. One of its key methods is httpx.delete()
, which sends HTTP DELETE requests. This guide will explain how to use it effectively.
Table Of Contents
What is httpx.delete()?
The httpx.delete()
method is used to send HTTP DELETE requests. DELETE requests are typically used to remove resources from a server. This method is part of the httpx
library, which is a modern alternative to requests
.
If you're new to httpx
, you might want to check out our guide on how to install httpx in Python.
Basic Syntax of httpx.delete()
The basic syntax for httpx.delete()
is straightforward. Here's how you can use it:
import httpx
response = httpx.delete('https://example.com/resource/123')
print(response.status_code)
In this example, we send a DELETE request to remove a resource with ID 123 from the server. The server responds with a status code, which we print out.
Handling Responses
When you send a DELETE request, the server responds with a status code. Common status codes include:
- 200 OK: The request was successful.
- 204 No Content: The request was successful, and there is no content to return.
- 404 Not Found: The resource was not found.
You can handle these responses in your code to ensure your application behaves correctly. For example:
import httpx
response = httpx.delete('https://example.com/resource/123')
if response.status_code == 200:
print("Resource deleted successfully.")
elif response.status_code == 404:
print("Resource not found.")
else:
print("An error occurred.")
Adding Headers and Parameters
Sometimes, you may need to add headers or parameters to your DELETE request. This can be done using the headers
and params
arguments:
import httpx
headers = {'Authorization': 'Bearer YOUR_TOKEN'}
params = {'force': 'true'}
response = httpx.delete('https://example.com/resource/123', headers=headers, params=params)
print(response.status_code)
In this example, we add an authorization token and a query parameter to the request. This is useful when the server requires authentication or additional information.
Error Handling
Error handling is crucial when making HTTP requests. The httpx
library provides several ways to handle errors. For example, you can use a try-except
block to catch exceptions:
import httpx
try:
response = httpx.delete('https://example.com/resource/123')
response.raise_for_status()
except httpx.HTTPStatusError as e:
print(f"HTTP error occurred: {e}")
except httpx.RequestError as e:
print(f"Request error occurred: {e}")
This code attempts to send a DELETE request and raises an exception if the request fails. This ensures that your application can handle errors gracefully.
Conclusion
The httpx.delete()
method is a powerful tool for sending HTTP DELETE requests in Python. It is easy to use and integrates well with other httpx
methods like httpx.get() and httpx.post().
By following this guide, you should be able to use httpx.delete()
effectively in your projects. Remember to handle errors and responses properly to ensure your application runs smoothly.