How to Make an HTTP Request with Python
Python offers several ways to make HTTP requests. The most popular is the requests library, but the standard library's urllib also works.
GET request example
Making a GET request to fetch data with custom headers:
import requests
response = requests.get(
"https://api.example.com/users",
headers={"Accept": "application/json", "Authorization": "Bearer YOUR_TOKEN"}
)
data = response.json()POST request example
Making a POST request to send JSON data:
import requests
response = requests.post(
"https://api.example.com/users",
headers={"Content-Type": "application/json"},
json={"name": "John", "email": "john@example.com"}
)
print(response.status_code)Other libraries you can try
- Python:
Requests— simple and popular HTTP library,HTTPX— async-capable HTTP client - JavaScript:
Axios— promise-based HTTP client,node-fetch— Fetch API for Node.js - Ruby:
HTTParty— simple HTTP client,Faraday— flexible HTTP library,RestClient— lightweight REST client - Java:
OkHttp— efficient HTTP client,Apache HttpClient— feature-rich library - C#:
RestSharp— simple REST client,Flurl— fluent HTTP library - Go:
resty— simple HTTP client,grequests— goroutine-based requests - PHP:
Guzzle— popular PHP HTTP client,Symfony HttpClient— component-based HTTP client