In Python, you can send HTTP requests using the “requests” module. The “requests” module simplifies the process of working with HTTP requests in Python by providing a higher-level interface to send HTTP requests.
To send an HTTP request in Python, you need to import the “requests” module and call the appropriate function for the request type you want to send (e.g., “get()” for a GET request, “post()” for a POST request).
GET Request
Here is an example of sending a GET request in Python:
import requests
response = requests.get("https://www.example.com")
print(response.text)
In this example, we use the “get()” function of the “requests” module to send a GET request to the “https://www.example.com” URL. The “get()” function returns a “Response” object, which contains the response data. You can access the response data using the “text” attribute of the “Response” object.
You can also send POST, PUT, DELETE, and other HTTP request types using the “post()”, “put()”, “delete()”, and other functions of the “requests” module.
POST Request
Here is an example of sending a POST request in Python:
import requests
data = {
"name": "John Smith",
"age": 30
}
response = requests.post("https://www.example.com", data=data)
print(response.text)
In this example, we use the “post()” function of the “requests” module to send a POST request to the “https://www.example.com” URL with a “name” and “age” field in the request body.
You can also set headers, cookies, and other request options using the “headers”, “cookies”, and other arguments of the “requests” functions.
Exercises
Here are some exercises with solutions to help you practice what you just learned:
Write a Python program that sends a GET request to the “https://api.github.com/repos/python/cpython” URL and prints the name and description of the repository.
import requests
response = requests.get("https://api.github.com/repos/python/cpython")
data = response.json()
print(data["name"])
print(data["description"])
Write a Python program that sends a POST request to the “https://httpbin.org/post” URL with a “name” and “age” field in the request body, and prints the received data.
import requests
data = {
"name": "John Smith",
"age": 30
}
response = requests.post("https://httpbin.org/post", data=data)
print(response.json())
Write a Python program that sends a DELETE request to the “https://httpbin.org/delete” URL and prints the received data.
import requests
response = requests.delete("https://httpbin.org/delete")
print(response.json())
Write a Python program that sends a PUT request to the “https://httpbin.org/put” URL with a “name” and “age” field in the request body, and prints the received data.
import requests
import json
data = {
"name": "John Smith",
"age": 30
}
headers = {
"Content-Type": "application/json"
}
response = requests.put("https://httpbin.org/put", json=data, headers=headers)
print(response.json())
Write a Python program that sends a GET request to the “https://api.github.com/search/repositories” URL with “q” and “sort” query string parameters, and prints the names and descriptions of the top 10 repositories.
import requests
import json
params = {
"q": "language:python",
"sort": "stars"
}
response = requests.get("https://api.github.com/search/repositories", params=params)
data = response.json()
for item in data["items"][:10]:
print(item["name"])
print(item["description"])
print()