Request - Making API calls

Python - A Quick Start for existing Programers

1 min read

Published Sep 16 2025, updated Sep 30 2025


21
0
0
0

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 requests

To include the module:

import requests




Key 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 request
response = requests.get("https://api.github.com")
print(response.status_code)
# HTTP status code
print(response.headers["Content-Type"])
print(response.text[:100])
# first 100 chars of response body

# GET with query parameters
params = {"q": "python requests", "page": 1}
response = requests.get("https://www.example.com/search", params=params)
print(response.url)
# URL with encoded query string
print(response.json())
# if the response is JSON

# POST request with data
data = {"username": "alice", "password": "1234"}
response = requests.post("https://httpbin.org/post", data=data)
print(response.json())

# Sending JSON
json_data = {"title": "Hello", "body": "This is a post"}
response = requests.post("https://httpbin.org/post", json=json_data)
print(response.json())

# Custom headers
headers = {"User-Agent": "MyApp/1.0"}
response = requests.get("https://httpbin.org/headers", headers=headers)
print(response.json())

# Handling timeouts
try:
    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 request
response = requests.delete(f"{url}/delete")
print("DELETE status:", response.status_code)
print("DELETE response:", response.json())


# Post file data
# Open the file in binary mode
with 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 response
print("Status code:", response.status_code)
print("Response JSON:", response.json())

# Multiple files
with 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())
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact