Python Requests Module – HTTP Request

The requests module in Python is a popular library for making HTTP requests. It provides easy methods to send HTTP requests like GET, POST, PUT, DELETE, and more.

Installation of requests Module

Before using the requests module, you need to install it if you haven’t already:

pip install requests

1. Making a Simple GET Request

A GET request is used to retrieve data from a server. It does not modify any data on the server and is the most common type of HTTP request.

import requests

url = "https://jsonplaceholder.typicode.com/posts/1"
response = requests.get(url)

print("Status Code:", response.status_code)
print("Response Text:", response.text)
print("JSON Response:", response.json())

Explanation:

  • The requests.get(url) function sends a GET request to fetch data.
  • response.status_code returns the HTTP status code (200 for success).
  • response.text returns the full response as a string.
  • response.json() converts the response into a Python dictionary.

Output:

Status Code: 200
Response Text: 
{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit..."
}
JSON Response: {'userId': 1, 'id': 1, 'title': 'sunt aut facere...', 'body': 'quia et suscipit\nsuscipit...'}

2. Sending a POST Request

A POST request is used to send data to a server to create a new resource. This is commonly used for submitting forms or adding new records to a database.

import requests

url = "https://jsonplaceholder.typicode.com/posts"
data = {"title": "Python Requests", "body": "Learning HTTP requests in Python", "userId": 1}
response = requests.post(url, json=data)

print("Status Code:", response.status_code)
print("Response JSON:", response.json())

Explanation:

  • requests.post(url, json=data) it sends a post request with data as payload.
  • Json= data – automatically converts into Json format dictionary.
  • response.json() Returns JSON response from the server.

Output:

Status Code: 201
Response JSON: {'title': 'Python Requests', 'body': 'Learning HTTP requests in Python', 'userId': 1, 'id': 101}

(201 means the resource was successfully created.)

3. Sending a PUT Request

A PUT request updates an existing resource on the server. Unlike POST, it modifies an existing entry instead of creating a new one.

import requests

url = "https://jsonplaceholder.typicode.com/posts/1"
data = {"title": "Updated Title", "body": "Updated Body", "userId": 1}
response = requests.put(url, json=data)

print("Status Code:", response.status_code)
print("Response JSON:", response.json())

Explanation:

  • requests.put(url, json=data) updates the resource at the given URL.
  • The response contains the updated data.

Output:

Status Code: 200
Response JSON: {'title': 'Updated Title', 'body': 'Updated Body', 'userId': 1, 'id': 1}

(200 means the update was successful.)

4. Sending a DELETE Request

A DELETE request is used to remove a resource from the server.

import requests

url = "https://jsonplaceholder.typicode.com/posts/1"
response = requests.delete(url)

print("Status Code:", response.status_code)

Explanation:

  • requests.delete(url) sends a request to delete the resource.
  • Usually, the response status code is 200 or 204 if the deletion is successful.

Output:

Status Code: 200

5. Using Request Headers

Headers provide additional metadata to the request, such as authentication tokens, content types, or user-agent information.

import requests

url = "https://jsonplaceholder.typicode.com/posts/1"
headers = {"User-Agent": "my-app"}
response = requests.get(url, headers=headers)

print("Status Code:", response.status_code)
print("Response JSON:", response.json())

Explanation:

  • headers={"User-Agent": "my-app"} sends a custom User-Agent in the request.
  • Some servers require headers for authentication or special access.

Output:

Status Code: 200
Response JSON: {'userId': 1, 'id': 1, 'title': 'sunt aut facere...', 'body': 'quia et suscipit\nsuscipit...'}

6. Handling Response Headers

Response headers provide metadata about the server’s response, such as content type, caching policies, and server information.

import requests

url = "https://jsonplaceholder.typicode.com/posts/1"
response = requests.get(url)

print("Response Headers:", response.headers)

Explanation:

  • response.headers returns a dictionary containing all response headers.
  • Headers include Content-Type, Server, Date, etc.

Output:

Response Headers: {'Content-Type': 'application/json; charset=utf-8', 'Server': 'cloudflare', ...}

7. Handling Timeouts

Timeouts prevent requests from hanging indefinitely. If the server does not respond within the given time, an exception is raised.

import requests

url = "https://jsonplaceholder.typicode.com/posts/1"
try:
    response = requests.get(url, timeout=3)  # Waits for 3 seconds
    print("Response Status Code:", response.status_code)
except requests.exceptions.Timeout:
    print("Request Timed Out!")

Explanation:

  • requests.get(url, timeout=3) sets a timeout of 3 seconds.
  • If the server doesn’t respond in 3 seconds, an exception is raised.

Output:

Response Status Code: 200

or if the server is slow:

Request Timed Out!

8. Handling Exceptions

Network issues can cause errors, and handling them prevents your program from crashing.

import requests

url = "https://invalid-url.com"
try:
    response = requests.get(url)
    print("Response Status Code:", response.status_code)
except requests.exceptions.RequestException as e:
    print("Error:", e)

Explanation:

  • try-except block catches exceptions such as:
    • requests.exceptions.ConnectionError (no internet)
    • requests.exceptions.Timeout (server is slow)
    • requests.exceptions.HTTPError (bad response)
  • requests.exceptions.RequestException is a general exception.

Output:

Error: HTTPSConnectionPool(host='invalid-url.com', port=443): Max retries exceeded...

Conclusion

The requests module helps send HTTP requests quite easily. Main takeaways:

  • Use requests.get(), requests.post(), requests.put(), and requests.delete().
  • Handle response codes and JSON responses.
  • Use headers and timeouts to enhance requests.
  • Handle errors using try-except.