Requests Cheatsheet¶

Author: Thomas Uhuru¶

A concise guide for using Python's requests library — perfect for APIs, automation, and web data fetching.

In [ ]:
import requests

Making Requests¶

In [ ]:
# GET - Retrieve data from a resource
r = requests.get('https://jsonplaceholder.typicode.com/users/1')
print(r.json())
In [ ]:
# POST - Submit data to be processed
r = requests.post('https://httpbin.org/post', data={'key':'value'})
print(r.json())
In [ ]:
# PUT - Update a resource
r = requests.put('https://httpbin.org/put', data={'key':'updated_value'})
print(r.json())
In [ ]:
# DELETE - Delete a resource
r = requests.delete('https://httpbin.org/delete')
print(r.status_code)
In [ ]:
# HEAD - Retrieve only headers
r = requests.head('https://httpbin.org/get')
print(r.headers)
In [ ]:
# OPTIONS - Get supported methods
r = requests.options('https://httpbin.org/get')
print(r.headers.get('Allow'))

Passing Parameters¶

In [ ]:
# Query String Parameters
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('https://httpbin.org/get', params=payload)
print(r.url)
In [ ]:
# Form Data
payload = {'username': 'user', 'password': 'pass'}
r = requests.post('https://httpbin.org/post', data=payload)
print(r.json())
In [ ]:
# JSON Body
payload = {'title': 'New Post', 'body': 'Content'}
r = requests.post('https://jsonplaceholder.typicode.com/posts', json=payload)
print(r.json())
In [ ]:
# Custom Headers
headers = {'User-Agent': 'My-App/1.0'}
r = requests.get('https://httpbin.org/get', headers=headers)
print(r.json())

Handling the Response Object¶

In [ ]:
# Check Status and Content
r = requests.get('https://jsonplaceholder.typicode.com/users/1')
print(r.status_code)
print(r.ok)
print(r.url)
print(r.text[:200])
In [ ]:
# Access JSON and Headers
r = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(r.json())
print(r.headers)
In [ ]:
# Raise for Status
r = requests.get('https://jsonplaceholder.typicode.com/invalid')
try:
    r.raise_for_status()
except requests.exceptions.HTTPError as e:
    print('Error:', e)

Advanced Features¶

In [ ]:
# Authentication
r = requests.get('https://httpbin.org/basic-auth/user/pass', auth=('user', 'pass'))
print(r.json())
In [ ]:
# Timeouts
r = requests.get('https://httpbin.org/delay/2', timeout=3)
print(r.status_code)
In [ ]:
# Redirects
r = requests.get('https://httpbin.org/redirect/1', allow_redirects=False)
print(r.status_code)
In [ ]:
# File Uploads
with open('sample.txt', 'w') as f:
    f.write("Example upload file.")

files = {'file': open('sample.txt', 'rb')}
r = requests.post('https://httpbin.org/post', files=files)
print(r.status_code)
files['file'].close()
In [ ]:
# Proxies
proxies = {'http': 'http://10.10.1.10:3128'}
# Example setup (disabled to prevent errors)
print('Proxy example setup complete (commented out).')

Sessions (Persistence)¶

In [ ]:
# Create a session
s = requests.Session()
s.headers.update({'x-client-id': 'app-id'})
In [ ]:
# Persist cookies across requests
r1 = s.get('https://httpbin.org/cookies/set/sessioncookie/123')
r2 = s.get('https://httpbin.org/cookies')
print(r2.json())
In [ ]:
# Close session
s.close()