python requests HTTP API web

The requests library in Python

Learning objectives

By the end of this pill you will be able to:

  • understand the main HTTP methods;
  • use the requests library to send HTTP requests (GET and POST);
  • manage custom headers, cookies and sessions;
  • analyse the status code and response content.

Recap: HTTP methods

HTTP methods (or verbs) specify the action the client wants to perform on the server.

  • GET: requests data from the server (e.g. a page or an API).
  • POST: sends data to the server (e.g. a form or JSON).
  • PUT: updates an existing resource.
  • DELETE: deletes a resource.
  • HEAD, PATCH, OPTIONS: used for special cases.

Note: The requests library greatly simplifies working with HTTP methods, handling connections, encoding and redirects internally.

GET and POST requests

First, install the library (if needed) with pip install requests.

import requests

# GET example
r = requests.get("https://api.github.com")
print(r.status_code)    # 200 = OK
print(r.headers["content-type"])
print(r.text[:200])     # first 200 characters of the response

# POST example with form parameters
data = {"user": "mario", "score": 42}
r = requests.post("https://httpbin.org/post", data=data)
print(r.json())  # parse JSON response

Custom headers

You can add headers to provide information to the server.

headers = {
    "User-Agent": "PillolaRequests/1.0",
    "Accept": "application/json"
}
r = requests.get("https://httpbin.org/headers", headers=headers)
print(r.json())

Handling Cookies

HTTP requests can send and receive cookies.

# Sending cookies to the server
cookies = {"session_id": "ABC123"}
r = requests.get("https://httpbin.org/cookies", cookies=cookies)
print(r.json())

# Reading cookies received
print(r.cookies)

Using Sessions

Sessions automatically maintain cookies and settings across multiple requests (like a “browsing session”).

s = requests.Session()
s.headers.update({"User-Agent": "PillolaSession/1.0"})

# First request: sets a cookie
s.get("https://httpbin.org/cookies/set/user/emiliano")

# Second request: the cookie is maintained
r = s.get("https://httpbin.org/cookies")
print(r.json())

Analysing the response

Every Response object includes useful information:

  • r.status_code: status code (200, 404, 500…);
  • r.text: response body as a string;
  • r.json(): automatic decoding if in JSON format;
  • r.headers: dictionary of received headers;
  • r.cookies: any cookies returned by the server.

Note: Always check r.ok to quickly verify whether the request succeeded (True for status 200–399).


In brief: the requests library is a powerful and intuitive tool for communicating with web APIs. Use sessions for persistent authentication and headers/cookies to simulate real browser requests.