Using Python Requests to Send RESTful API Requests: A Step‑by‑Step Guide
This article introduces the fundamentals of RESTful APIs and demonstrates how to install the Python requests library, send GET, POST, PUT, and DELETE requests, set custom headers, handle responses, manage sessions, and apply best‑practice tips for reliable API testing.
1. Introduction to RESTful APIs
RESTful APIs follow the Representational State Transfer (REST) architectural style and use standard HTTP methods (GET, POST, PUT, DELETE) to operate on resources. Core constraints include resource orientation (each resource has a unique URI), statelessness (each request is independent), a uniform interface (HTTP methods map to actions), HATEOAS (responses contain links for discoverability), client‑side caching, and a layered system.
2. Using the requests library to send RESTful requests
2.1 Install requests
pip install requests2.2 Send a GET request
import requests
url = "https://api.example.com/users"
response = requests.get(url)
print(response.status_code) # print status code
print(response.json()) # print JSON response body2.3 Send a POST request
import requests
url = "https://api.example.com/users"
data = {"name": "John Doe", "email": "[email protected]"}
response = requests.post(url, json=data)
print(response.status_code) # print status code
print(response.json()) # print JSON response body2.4 Send a PUT request
import requests
url = "https://api.example.com/users/123"
data = {"name": "John Doe", "email": "[email protected]"}
response = requests.put(url, json=data)
print(response.status_code) # print status code
print(response.json()) # print JSON response body2.5 Send a DELETE request
import requests
url = "https://api.example.com/users/123"
response = requests.delete(url)
print(response.status_code) # print status code
print(response.json()) # print JSON response body3. Setting request headers
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_ACCESS_TOKEN"
}
response = requests.get(url, headers=headers)
print(response.json())4. Handling responses
After a request, the response object provides:
Status code: response.status_code
Headers: response.headers
Body: response.text or response.json()
5. Using a Session to manage a persistent connection
import requests
session = requests.Session()
session.headers.update({"Content-Type": "application/json"})
response1 = session.get("https://api.example.com/users")
response2 = session.post("https://api.example.com/users", json={"name": "John"})6. Full example combining all methods
# GET request
response = requests.get("https://api.example.com/users")
print(response.json())
# POST request
data = {"name": "John Doe", "email": "[email protected]"}
response = requests.post("https://api.example.com/users", json=data)
print(response.json())
# PUT request
data = {"name": "John Doe", "email": "[email protected]"}
response = requests.put("https://api.example.com/users/123", json=data)
print(response.json())
# DELETE request
response = requests.delete("https://api.example.com/users/123")
print(response.json())7. Important considerations
Check status codes (e.g., 200, 404, 500) to determine success.
Handle exceptions such as network errors or timeouts.
Ensure request and response data formats match (typically JSON).
Conclusion
Using the requests library makes it straightforward to automate RESTful API testing. Mastering GET, POST, PUT, and DELETE operations, along with header configuration and response handling, enables efficient and reliable interface testing.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.