Requests
Requests is a popular third-party Python library used for making HTTP requests simple and human-friendly. It abstracts low-level networking details and provides a clean API for interacting with web services and REST APIs.
Official site: https://docs.python-requests.org
Installation
bash
pip install requestsMaking HTTP Requests
GET Request
python
response = requests.get("https://api.example.com/data")POST Request
python
response = requests.post("https://api.example.com/create", data={"key": "value"})Other HTTP Methods
python
requests.put(url, data=data)
requests.delete(url)
requests.patch(url, data=data)
requests.head(url)
requests.options(url)Request Parameters
Query Parameters
python
params = {"page": 1, "limit": 10}
requests.get(url, params=params)Headers
python
headers = {"Authorization": "Bearer TOKEN"}
requests.get(url, headers=headers)Request Body (Form Data)
python
data = {"username": "admin", "password": "1234"}
requests.post(url, data=data)JSON Body
python
requests.post(url, json={"title": "Python"})Timeout Handling
python
requests.get(url, timeout=5)Prevents indefinite waiting for a server response.
Response Object
Status Code and Headers
python
response.status_code
response.headersResponse Content
python
response.text # string
response.content # bytesJSON Response
python
data = response.json()URL and History
python
response.url
response.history # redirectsAuthentication
Basic Authentication
python
from requests.auth import HTTPBasicAuth
requests.get(url, auth=HTTPBasicAuth("user", "pass"))Token-Based Authentication
python
headers = {"Authorization": "Bearer TOKEN"}
requests.get(url, headers=headers)Error Handling
HTTP Errors
python
response.raise_for_status()Exception Handling
python
try:
requests.get(url, timeout=3)
response.raise_for_status()
except requests.exceptions.Timeout:
print("Request timed out")
except requests.exceptions.ConnectionError:
print("Connection error")
except requests.exceptions.HTTPError as e:
print(e)
except requests.exceptions.RequestException as e:
print(e)
except requests.exceptions.TooManyRedirects as e:
print(e)Working with Files
File Upload
python
files = {"file": open("data.txt", "rb")}
requests.post(url, files=files)File Download
python
response = requests.get(file_url)
with open("file.pdf", "wb") as f:
f.write(response.content)Sessions (Persistent Connections)
Sessions persist headers, cookies, and connections across requests.
python
session = requests.Session()
# Applied to all future requests made with this session
session.headers.update({
"Authorization": "Bearer TOKEN",
"User-Agent": "MyApp/1.0"
})
response1 = session.get("https://api.example.com/profile")
response2 = session.post("https://api.example.com/update", json={"name": "Alice"})Both requests automatically include the same headers without passing them again.
Cookies
Sending Cookies
python
cookies = {"session_id": "abc123"}
requests.get(url, cookies=cookies)Reading Cookies
python
response.cookiesRedirects
python
requests.get(url, allow_redirects=True)Check redirect history:
python
response.historySSL Verification
python
requests.get(url, verify=True) # default
requests.get(url, verify=False) # not recommendedCustom CA bundle:
python
requests.get(url, verify="ca.pem")Proxies
python
proxies = {
"http": "http://proxy.example.com:8080",
"https": "https://proxy.example.com:8080",
}
requests.get(url, proxies=proxies)Streaming Large Responses
python
response = requests.get(url, stream=True)
for chunk in response.iter_content(chunk_size=1024):
process(chunk)Limitations
- Not asynchronous (use
httpxoraiohttpfor async). - No built-in retry logic (can be added with adapters)
- Not designed for browser automation.
- No built-in rate limiting.
