Consuming APIs - GET and POST
In this chapter, we will focus on how to consume REST APIs using Python, specifically using the requests
library. We will cover how to perform GET and POST requests, which are fundamental for retrieving and sending data to APIs.
Overviewβ
- GET Requests: Used to retrieve data from a server.
- POST Requests: Used to send data to a server to create a new resource.
Prerequisitesβ
Ensure you have the requests
library installed. If not, install it using:
pip install requests
Making GET Requestsβ
GET requests are used to retrieve data from a REST API. This method should not change the state of the server.
Example: Fetching Dataβ
Let's say you want to retrieve a list of users from a hypothetical API:
import requests
# Define the API endpoint
url = 'https://api.example.com/users'
# Send the GET request
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
data = response.json() # Parse JSON data
print("Users retrieved successfully:")
for user in data:
print(f"User ID: {user['id']}, Name: {user['name']}")
else:
print(f"Failed to retrieve data: {response.status_code}")
Explanationβ
- URL: The endpoint from which we want to fetch data.
- GET Request: Use
requests.get()
to send a GET request. - Response Handling: Check if the status code is
200 OK
. Useresponse.json()
to parse the JSON response into a Python dictionary.
Making POST Requestsβ
POST requests are used to send data to a server to create a new resource. This method often requires sending data in the body of the request.
Example: Creating a New Resourceβ
Letβs create a new user with a POST request:
import requests
# Define the API endpoint
url = 'https://api.example.com/users'
# Define the data to send
payload = {
'name': 'John Doe',
'email': 'john.doe@example.com'
}
# Send the POST request
response = requests.post(url, json=payload)
# Check if the request was successful
if response.status_code == 201:
print("User created successfully!")
created_user = response.json() # Get the created user data
print(f"User ID: {created_user['id']}, Name: {created_user['name']}")
else:
print(f"Failed to create user: {response.status_code}")
Explanationβ
- URL: The endpoint where the data will be sent.
- Payload: Data to be sent in the request body, typically in JSON format.
- POST Request: Use
requests.post()
to send a POST request. Thejson
parameter automatically sets theContent-Type
header toapplication/json
and serializes the data. - Response Handling: Check if the status code is
201 Created
. Useresponse.json()
to parse the response and access the data of the newly created resource.
Advanced Usageβ
Query Parametersβ
Sometimes you may need to pass query parameters with your GET request. For example, to filter users:
import requests
# Define the API endpoint with query parameters
url = 'https://api.example.com/users'
params = {'status': 'active'}
# Send the GET request with parameters
response = requests.get(url, params=params)
# Handle the response
if response.status_code == 200:
data = response.json()
print("Active users retrieved:")
for user in data:
print(f"User ID: {user['id']}, Name: {user['name']}")
else:
print(f"Failed to retrieve data: {response.status_code}")
Custom Headersβ
You might need to send custom headers with your requests. For example, including an API key:
import requests
# Define the API endpoint
url = 'https://api.example.com/users'
# Define custom headers
headers = {
'Authorization': 'Bearer YOUR_API_KEY',
'Accept': 'application/json'
}
# Send the GET request with headers
response = requests.get(url, headers=headers)
# Handle the response
if response.status_code == 200:
data = response.json()
print("Data retrieved with custom headers:")
print(data)
else:
print(f"Failed to retrieve data: {response.status_code}")
Summaryβ
In this chapter, we have learned how to make GET and POST requests using Python's requests
library. You can now retrieve data from an API and send data to create new resources. These operations are fundamental for interacting with RESTful services.
In the next chapter, we will cover additional HTTP methods (PUT, PATCH, DELETE) for more advanced interactions with REST APIs. Stay tuned!