Request - Making API calls
Python - A Quick Start for existing Programers
1 min read
This section is 1 min read, full guide is 44 min read
Published Sep 16 2025, updated Aug 17 2026
21
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
Python
The requests module is the most popular library for making HTTP requests in Python. It simplifies interacting with web APIs, downloading web content, sending data, etc.
To install:
pip install requestsTo include the module:
import requestsKey features:
- Send HTTP/HTTPS requests:
GET,POST,PUT,DELETE, etc. - Handle query parameters, headers, cookies.
- Automatic JSON decoding.
- Handle timeouts, redirects, and sessions.
- Supports file uploads.
Main functions
requests.get(url): Simple GET request.requests.post(url): POST request.params=: Dictionary of URL query parameters.data=: Dictionary of form data (POST).json=: Dictionary to send as JSON payload.headers=: Custom headers.cookies=: Send cookies.timeout=: Timeout in seconds.response.status_code: HTTP response code.response.text: Response content as string.response.json(): Parse JSON response.requests.Session(): Maintain cookies and headers across requests.
Examples
import requests# Simple GET requestresponse = requests.get("https://api.github.com")print(response.status_code) # HTTP status codeprint(response.headers["Content-Type"])print(response.text[:100]) # first 100 chars of response body# GET with query parametersparams = {"q": "python requests", "page": 1}response = requests.get("https://www.example.com/search", params=params)print(response.url) # URL with encoded query stringprint(response.json()) # if the response is JSON# POST request with datadata = {"username": "alice", "password": "1234"}response = requests.post("https://httpbin.org/post", data=data)print(response.json())# Sending JSONjson_data = {"title": "Hello", "body": "This is a post"}response = requests.post("https://httpbin.org/post", json=json_data)print(response.json())# Custom headersheaders = {"User-Agent": "MyApp/1.0"}response = requests.get("https://httpbin.org/headers", headers=headers)print(response.json())# Handling timeoutstry: response = requests.get("https://httpbin.org/delay/5", timeout=2)except requests.Timeout: print("Request timed out!")# Sessions (persistent cookies, headers)session = requests.Session()session.headers.update({"Authorization": "Bearer TOKEN"})response = session.get("https://httpbin.org/headers")print(response.json())url = "https://httpbin.org"# PUT request (replace/update resource)put_data = {"name": "Alice", "role": "admin"}response = requests.put(f"{url}/put", json=put_data)print("PUT status:", response.status_code)print("PUT response:", response.json())# PATCH request (partial update)patch_data = {"role": "moderator"}response = requests.patch(f"{url}/patch", json=patch_data)print("PATCH status:", response.status_code)print("PATCH response:", response.json())# DELETE requestresponse = requests.delete(f"{url}/delete")print("DELETE status:", response.status_code)print("DELETE response:", response.json())# Post file data# Open the file in binary modewith open("example.txt", "rb") as f: files = {"file": f} # 'file' is the form field name response = requests.post(f"{url}/post", files=files)# Check the responseprint("Status code:", response.status_code)print("Response JSON:", response.json())# Multiple fileswith open("example1.txt", "rb") as f1, open("example2.txt", "rb") as f2: files = { "file1": f1, "file2": f2 } data = {"user": "alice", "role": "admin"} # extra form fields response = requests.post(f"{url}/post", files=files, data=data)print(response.json())