PYTHON Reference Guide
Revision Time: 3 mins

Requests Reference

Making HTTP requests — GET, POST, headers, timeouts, and error handling.

requests.get()
Return: Response

Send an HTTP GET request and return the response.

Used In: Fetching REST API data, downloading JSON payloads.

Syntax signature:requests.get(url, params=None, headers=None, timeout=None)
Code snippet:
python
import requests
r = requests.get(
    'https://api.github.com/repos/python/cpython',
    timeout=10
)
print(r.status_code)
Expected Output:200

Time Complexity: Depends on network

Common Mistakes: Not setting timeout — can block a production process indefinitely.

Related Methods: r.json(), r.raise_for_status()

Remember: Always set a timeout — without it, the call blocks indefinitely if the server is unresponsive.
requests.post()
Return: Response

Send an HTTP POST request with JSON or form data.

Used In: Sending events to webhooks, submitting API payloads.

Syntax signature:requests.post(url, json=None, data=None, headers=None, timeout=None)
Code snippet:
python
import requests
payload = {'user': 'Alice', 'action': 'login'}
r = requests.post(
    'https://httpbin.org/post',
    json=payload,
    timeout=10
)
print(r.status_code)
Expected Output:200

Time Complexity: Depends on network

Related Methods: r.json()

Remember: Use json=payload to auto-set Content-Type: application/json. Use data= for form-encoded bodies.
params (query string)
Return: Response

Pass query string parameters as a dict — requests encodes them automatically.

Used In: Paginated API calls, filtered data fetching.

Syntax signature:requests.get(url, params={'key': 'value'})
Code snippet:
python
import requests
r = requests.get(
    'https://httpbin.org/get',
    params={'page': 1, 'limit': 20},
    timeout=5
)
print(r.url)
Expected Output:https://httpbin.org/get?page=1&limit=20

Time Complexity: O(N) encoding

Remember: requests handles URL encoding automatically — never build query strings manually with f-strings.
headers
Return: Response

Add custom HTTP headers — authentication, content type, API tokens.

Used In: Authenticated API calls, custom content negotiation.

Syntax signature:requests.get(url, headers={'Authorization': 'Bearer TOKEN'})
Code snippet:
python
import requests
headers = {
    'Authorization': 'Bearer my-api-token',
    'Accept': 'application/json'
}
r = requests.get('https://api.example.com/data', headers=headers, timeout=10)
print(r.status_code)
Expected Output:200

Time Complexity: O(1)

Remember: Never hardcode tokens in source code — load them from environment variables.
response.json()
Return: dict | list

Parse the JSON response body into a Python dict or list.

Used In: Extracting API response payloads.

Syntax signature:response.json()
Code snippet:
python
import requests
r = requests.get('https://httpbin.org/json', timeout=5)
data = r.json()
print(type(data))
Expected Output:<class 'dict'>

Time Complexity: O(N)

Related Methods: r.text, r.content

Remember: Raises requests.exceptions.JSONDecodeError if the response body is not valid JSON.
raise_for_status()
Return: None

Raise an HTTPError exception for 4xx and 5xx response status codes.

Used In: API error handling in ETL pipelines.

Syntax signature:response.raise_for_status()
Code snippet:
python
import requests
try:
    r = requests.get('https://httpbin.org/status/404', timeout=5)
    r.raise_for_status()
except requests.HTTPError as e:
    print(f'HTTP Error: {e.response.status_code}')
Expected Output:HTTP Error: 404

Time Complexity: O(1)

Common Mistakes: Checking r.status_code == 200 manually instead of using raise_for_status().

Related Methods: r.ok, r.status_code

Remember: Always call raise_for_status() before processing the response — never assume a request succeeded.
Session()
Return: Session

Reuse a persistent HTTP connection with shared headers and cookies across requests.

Used In: Multi-page API scraping, authenticated crawling.

Syntax signature:requests.Session()
Code snippet:
python
import requests
with requests.Session() as s:
    s.headers.update({'Authorization': 'Bearer TOKEN'})
    r1 = s.get('https://api.example.com/users', timeout=5)
    r2 = s.get('https://api.example.com/orders', timeout=5)
print(r1.status_code, r2.status_code)
Expected Output:200 200

Time Complexity: O(1) per request (connection reuse)

Related Methods: requests.get()

Remember: Session() reuses the underlying TCP connection — significantly faster for multiple requests to the same host.