Sending HTTP Requests with Requests
The Requests library simplifies sending HTTP requests, making it easy to interact with APIs and web servers. Here, we’ll explore various HTTP methods supported by Requests.
GET Requests: Retrieving Data from a Server
A GET request fetches data from a specified resource. It is often used to retrieve information from APIs or web pages.
Example: Simple GET Request
import requests
url = "https://jsonplaceholder.typicode.com/posts/1"
response = requests.get(url)
# Accessing the response
print("Status Code:", response.status_code)
print("Response JSON:", response.json())
Adding Query Parameters
params = {"userId": 1}
response = requests.get("https://jsonplaceholder.typicode.com/posts", params=params)
print("URL with Params:", response.url)
print("Response JSON:", response.json())
POST Requests: Sending Data to a Server
A POST request is used to send data to a server to create a new resource.
Example: Sending Data with POST
url = "https://jsonplaceholder.typicode.com/posts"
data = {
"title": "Python Requests",
"body": "Learning how to use POST requests",
"userId": 1
}
response = requests.post(url, json=data)
print("Status Code:", response.status_code)
print("Response JSON:", response.json())
PUT Requests: Updating a Resource
A PUT request updates an existing resource by replacing it with new data.
Example: Updating Data with PUT
url = "https://jsonplaceholder.typicode.com/posts/1"
data = {
"id": 1,
"title": "Updated Title",
"body": "Updated body content",
"userId": 1
}
response = requests.put(url, json=data)
print("Status Code:", response.status_code)
print("Response JSON:", response.json())
PATCH Requests: Partially Updating a Resource
A PATCH request updates part of an existing resource without replacing the entire data.
Example: Partial Update with PATCH
url = "https://jsonplaceholder.typicode.com/posts/1"
data = {"title": "Partially Updated Title"}
response = requests.patch(url, json=data)
print("Status Code:", response.status_code)
print("Response JSON:", response.json())
DELETE Requests: Removing a Resource
A DELETE request removes a resource from the server.
Example: Deleting a Resource
url = "https://jsonplaceholder.typicode.com/posts/1"
response = requests.delete(url)
print("Status Code:", response.status_code)
print("Response JSON:", response.text)
Try It Yourself
Problem 1: Fetch Posts by User ID
Write a script to retrieve all posts by user ID 2 from https://jsonplaceholder.typicode.com/posts
.
Show Solution
import requests
params = {"userId": 2}
response = requests.get("https://jsonplaceholder.typicode.com/posts", params=params)
print("Response JSON:", response.json())
Problem 2: Create a New Post
Write a script to create a new post with your own title and body content.
Show Solution
import requests
url = "https://jsonplaceholder.typicode.com/posts"
data = {
"title": "My First POST Request",
"body": "Exploring POST requests with Python",
"userId": 1
}
response = requests.post(url, json=data)
print("Response JSON:", response.json())
Understanding how to send various HTTP requests is crucial when working with web APIs. Practice these methods to build dynamic and interactive Python applications!