Code
python
import requests
# GET request
resp = requests.get("https://api.github.com/users/python", timeout=5)
print(resp.status_code)
print(resp.json())
# POST request
resp = requests.post("https://httpbin.org/post", json={"key": "value"})
# With params and headers
resp = requests.get(
"https://api.example.com/data",
params={"page": 1, "size": 20},
headers={"Authorization": "Bearer token"}
)
# Session to keep state
with requests.Session() as s:
s.headers.update({"Authorization": "Bearer token"})
s.get("https://api.example.com/data")