How to make HTTP requests in Python

Answered
Jan 05, 2026 640 views 1 answers
9

What library should I use for making HTTP requests in Python and how do I use it?

L
Asked by layla_web
Bronze 276 rep

1 Answer

19

Using requests library

import requests

# GET request
response = requests.get('https://api.example.com/data')
data = response.json()

# POST request with data
response = requests.post('https://api.example.com/submit',
    json={'name': 'John', 'email': 'john@example.com'},
    headers={'Authorization': 'Bearer token'}
)

# Handle errors
response.raise_for_status()  # Raises exception for 4xx/5xx

With error handling

try:
    response = requests.get(url, timeout=10)
    response.raise_for_status()
except requests.RequestException as e:
    print(f"Request failed: {e}")
D
Answered by devops_guru 1 week, 2 days ago
Gold 290 rep

Your Answer

You need to be logged in to answer questions.

Log In to Answer

Related Questions

Hot Questions

No hot questions available.