Requests
The `requests` library makes HTTP requests simple and intuitive. It is the go‑to library for interacting with web services and APIs.
Installation
`pip install requests`
GET Request
Fetch data from a URL.
import requests
response = requests.get("https://api.github.com")
print(response.status_code) # 200
data = response.json()
print(data["current_user_url"])
POST Request
Send data to the server (JSON or form).
payload = {"key": "value"}
response = requests.post("https://httpbin.org/post", json=payload)
print(response.json())
Headers and Authentication
Add headers or basic auth.
headers = {"Authorization": "Bearer token"}
response = requests.get("https://api.example.com/data", headers=headers)
Error Handling
Check status and raise for HTTP errors.
response.raise_for_status() # raises HTTPError for 4xx/5xx
Sessions
Persist cookies and settings across requests.
session = requests.Session()
session.headers.update({"User-Agent": "my-app"})
response = session.get("https://example.com")
Two Minute Drill
- Requests simplifies HTTP calls.
- Use `get()`, `post()`, `put()`, `delete()`.
- Access JSON with `.json()`.
- Add parameters, headers, authentication easily.
- Use sessions for persistent settings.
Need more clarification?
Drop us an email at career@quipoinfotech.com
