requests (HTTP)
requests is the de-facto HTTP client for Python. The standard library has urllib, but almost nobody uses it directly — requests is just nicer.
Install
SHELL
pip install requests
GET
PYTHON
import requests
r = requests.get('https://httpbin.org/get', params={'q': 'python'})
print(r.status_code) # 200
print(r.json()) # parsed body
POST a JSON body
PYTHON
r = requests.post(
'https://httpbin.org/post',
json={'name': 'Ada', 'role': 'admin'},
timeout=10,
)
r.raise_for_status()
Headers, auth, cookies
PYTHON
r = requests.get(
'https://api.github.com/user',
headers={'Accept': 'application/vnd.github+json'},
auth=('user', 'token'),
)
Session — re-use connections, cookies, defaults
PYTHON
with requests.Session() as s:
s.headers.update({'Authorization': 'Bearer ' + TOKEN})
me = s.get('https://api.example.com/me').json()
todo = s.get('https://api.example.com/todos').json()
Always use timeouts
PYTHON
requests.get(url, timeout=10) # hard 10s cap requests.get(url, timeout=(3, 10)) # connect=3s, read=10s
Without a timeout, a stuck server can hang your program forever.
Modern alternatives
- httpx — async + sync, modern API, HTTP/2 support.
- aiohttp — asyncio-native.
- urllib3 — what requests itself uses underneath.
Tip: Call
r.raise_for_status() right after every request — it converts a 4xx/5xx into an exception so failures don't slip through.Example
Example
# import requests
# r = requests.get('https://httpbin.org/get', params={'q': 'python'})
# print(r.status_code, r.json()['args'])
print('requests is the de-facto HTTP client. pip install requests')
Try it Yourself »
Exercise
Pass this kwarg on every request to prevent hangs.
requests.get(url,
=10)
Seven letters.
Discussion
Loading…