Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Requests

Learning Goals

By the end of this section you will:

  • know how to retrieve data from an API using a GET request

  • know how to send data to an API using a POST request

  • know how to update data via an API using PUT and PATCH requests

  • understand how headers and parameters work

  • understand how to handle errors and timeouts

The requests library allows Python programs to send HTTP/HTTPS requests easily. It’s commonly used for:

GET Request

Use GET to retrieve data from a server.

import requests

response = requests.get("https://jsonplaceholder.typicode.com/posts/1", verify=False)
print(response.status_code)
print(response.json())

POST Request

You usually use POST to send data to a server (e.g., submit a form).

data = {
    "title": "foo",
    "body": "bar",
    "userId": 1
}

response = requests.post("https://jsonplaceholder.typicode.com/posts", json=data, verify=False)
print(response.status_code)
print(response.json())

PUT and PATCH Requests

Use PUT to replace the entire record.

put_data = {
    "id": 1,
    "title": "updated title",
    "body": "updated body",
    "userId": 1
}

response = requests.put("https://jsonplaceholder.typicode.com/posts/1", json=put_data, verify=False)
print(response.json())

Use PATCH to update only specific fields.

patch_data = {"title": "new title only"}

response = requests.patch("https://jsonplaceholder.typicode.com/posts/1", json=patch_data, verify=False)
print(response.json())

DELETE Request

Sends a DELETE request to remove a resource. Often used in admin interfaces or APIs with write access.

response = requests.delete("https://jsonplaceholder.typicode.com/posts/1", verify=False)
print(response.status_code)

When to Use DELETE

You use a DELETE request when:

Caution

Example with Authentication (optional)

This shows how to send a DELETE request with an API token.

headers = {
    "Authorization": "Bearer YOUR_TOKEN"
}
response = requests.delete("https://api.example.com/posts/42", headers=headers, verify=False)
print(response.status_code)

Headers and Query Parameters

Headers are used for things like tokens, content-type, etc. Params are sent as a query string like ?userId=1

headers = {
    "Authorization": "Bearer abc123"
}
params = {
    "userId": 1
}

response = requests.get("https://jsonplaceholder.typicode.com/posts", headers=headers, params=params, verify=False)
print(response.url)
print(response.json())

Error Handling

try:
    response = requests.get("https://httpbin.org/status/404")
    response.raise_for_status()  # Raises HTTPError if status is 4xx or 5xx
except requests.exceptions.HTTPError as err:
    print("HTTP Error:", err)
except requests.exceptions.RequestException as err:
    print("Request Error:", err)

Timeouts

try:
    response = requests.get("https://httpbin.org/delay/3", timeout=2, verify=False)
except requests.exceptions.Timeout:
    print("Request timed out.")