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
Gold
•
290 rep
Your Answer
You need to be logged in to answer questions.
Log In to Answer