Sitemap
Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Getting into HTTP Requests?

HTTP Requests in Python made Easy

Guide to the Requests Library in Python:

8 min readJun 24, 2024

--

Press enter or click to view image in full size
Photo by Nathan Dumlao on Unsplash

In the dynamic and rapidly advancing domain of data engineering, professionals are frequently charged with the critical task of creating and managing robust data pipelines. These pipelines are essential for pulling data from diverse sources, processing it, and then storing it in formats that are easy to analyze and use for decision-making. A vital component of this workflow is the interaction with web services to either gather or dispatch data, which demands a dependable and efficient method for handling HTTP requests.

Enter the Python Requests library — a tool that greatly simplifies HTTP communication. Known for its straightforward and intuitive interface, the Requests module has become a popular choice among developers for many reasons. This guide will delve into the essential features of the Requests library, providing thorough examples and clear explanations to aid both novice and experienced programmers in mastering HTTP requests effortlessly.

About Me:

As an experienced developer with a passion for Python, I have seen firsthand how its versatility and simplicity can revolutionize data engineering. Python allows developers to streamline complex processes and enhance computational efficiency. I enjoy sharing my knowledge through insights on Python programming, covering everything from beginner tips to advanced techniques, as well as SQL, databases and AI advancements.

Committed to sharing my knowledge on Python programming, I provide insights ranging from novice advice to sophisticated methodologies, aiming to bolster the coding acumen of others and keep them updated with the latest advancements.
If you’re interested in more Python content, please consider following me. Alternatively, you can click here to check out my Python list on Medium

Why Use the Requests Module?
Before diving into the specifics, it’s important to understand why the Requests module is favored over alternatives like urllib:

  • Simplicity: The Requests module features a more straightforward API, requiring fewer lines of code to execute HTTP requests effectively.
  • Session Capabilities: It supports persistent sessions between requests, which is vital for tasks that involve multiple transactions with the same server.
  • Form Handling: Submitting form data is made simple with its intuitive methods.
  • JSON Response Handling: The library allows for easy retrieval and management of JSON data, a common format for web data interchange.
  • Exception Handling: It raises exceptions for bad status codes automatically, facilitating better error management and a smoother debugging process.

Installation of Requests

If you haven’t installed the requests module yet, you can easily do so using pip:

pip install requests

Basic Usage

To start using the requests module, you need to import it and then you can use its functions to send various types of HTTP requests.

import requests

Sending a GET Request

Here’s how to send a simple GET request to fetch data from a server:

import requests

# The URL to which the GET request will be sent.
url = 'https://api.example.com/data'
# Sending a GET request to the specified URL.
# The 'requests.get' function makes an HTTP GET request to the provided URL and stores the response in the 'response' variable.
response = requests.get(url)
# Printing the text content of the response.
# 'response.text' contains the body of the response from the server, typically in string format.
# This is useful for checking what data the server returned in response to the GET request.
print(response.text)

Checking the Response

After sending a request, it’s crucial to check the response:

# Check the HTTP status code of the response.
if response.status_code == 200:
# If the status code is 200, it indicates that the request was successful.
print('The request was successful!')
else:
# If the status code is not 200, the request failed.
# Print the status code to help diagnose the issue.
print('The request failed with status code:', response.status_code)

Query Parameters in GET Requests

If you need to send query parameters with your GET request:

# A dictionary containing query parameters for an HTTP GET request.
# These parameters will be appended to the URL as query strings.
params = {
'key1': 'value1',
'key2': 'value2'
}

# Sending a GET request to the specified URL. The 'params' dictionary is converted into query parameters.
# The Requests library automatically encodes these parameters and appends them to the URL.
response = requests.get(url, params=params)
# Printing the final URL after the query parameters have been appended.
# This is useful for debugging to verify that the URL has been constructed correctly.
print(response.url) # This will show the URL with the appended query parameters.

POST Requests

Sending data to a server is often done using a POST request. Here’s how you can do that with requests:

import requests 

# A dictionary containing the login credentials, typically a username and password.
data = {
'username': 'john',
'password': 'secret'
}
# Sending a POST request to the login endpoint of the API.
# The 'data' dictionary is passed as form-encoded data to the server.
response = requests.post('https://api.example.com/login', data=data)
# Printing the text of the response from the server, which might include details like login status or tokens.
print(response.text)
# A dictionary representing the data of a new article, including its title, content and associated tags.
json_data = {
'title': 'New Article',
'content': 'This is a new article',
'tags': ['python', 'requests']
}
# Sending a POST request to create a new article on the API.
# The 'json_data' dictionary is passed as JSON. The Requests library automatically sets the appropriate headers.
response = requests.post('https://api.example.com/articles', json=json_data)
# Printing the text of the response from the server, which could be details of the newly created article or an error message.
print(response.text)

Interested in data analysis and EDA with Pandas? Check out my articles on Medium for insights and tips in data science. Click here to view my Medium List.

Handling Response Content

Understanding how to handle different types of response content is crucial:

# Printing the text content of the response.
# 'response.text' contains the raw string response received from the server.
print(response.text)

# Parsing the JSON response.
# 'response.json()' converts the JSON formatted string in the response to a Python dictionary.
# This method is convenient for handling JSON data, which is common in REST APIs.
data = response.json()
# Printing the converted Python dictionary.
# This displays the JSON data structured as a dictionary, making it easier to access and manipulate specific data fields.
print(data)

Sessions

Using sessions can help you persist certain parameters across requests:

# Creating a session object using the 'requests' library. A session object allows for the persistence of certain parameters across multiple requests.
with requests.Session() as session:
# Updating the session's headers to include a custom header. Here, 'x-test' is set to 'true'.
# Custom headers like these can be used for a variety of purposes such as authentication tokens, indicating content types, or performing test requests.
session.headers.update({'x-test': 'true'})

# Using the session object to send a GET request to a specified URL.
# The session uses the updated headers for this request and any subsequent requests made with this session.
response = session.get('https://api.example.com/data')

# Printing the headers of the response received from the server.
# This can be useful to inspect what headers are returned by the server, which might include content type, server information, caching policies, etc.
print(response.headers)

Interested in more Python content? Click here to check out my list on Medium.

Handling Cookies

# Define the URL that the GET request will target.
url = 'https://example.com'

# Send a GET request to the specified URL and store the response object.
# This response object includes various data returned by the server, such as the status code, headers, and cookies.
response = requests.get(url)

# Print the cookies received from the server.
# The 'cookies' attribute of the response object contains cookies sent by the server as part of the HTTP response.
print(response.cookies)

# To send cookies to the server:
# Define a dictionary with cookies to send. Here, a 'session_id' cookie is set with the value '123456'.
cookie = {'session_id': '123456'}

# Send another GET request to the same URL, this time including the defined cookies with the request.
# The 'cookies' parameter allows you to send specific cookies to the server in the request.
response = requests.get(url, cookies=cookie)

# Print the text of the response.
# 'response.text' contains the body of the response as a string, which is useful for processing the content returned by the server.
print(response.text)

Timeouts

Setting timeouts is crucial to avoid hanging requests:

response = requests.get('https://api.example.com/data', timeout=5)  # Timeout after 5 seconds

Error Handling

Proper error handling can make your code more robust:

# Attempt to send a GET request to the specified URL with a timeout limit of 3 seconds.
# The timeout parameter ensures that the request will raise a Timeout exception if not completed within 3 seconds.
try:
response = requests.get(url, timeout=3)

# This method raises an HTTPError for bad responses (4xx or 5xx).
# By calling this after the GET request, you make sure to catch any responses that indicate server errors.
response.raise_for_status()

# Catching specific exceptions related to HTTP issues.
# If an HTTP error occurs (like 404 or 500), it will be caught here.
except requests.exceptions.HTTPError as errh:
print("Http Error:", errh)

# Exception handling for errors during the connection phase, like network problems.
except requests.exceptions.ConnectionError as errc:
print("Error Connecting:", errc)

# Catches timeout errors that occur if the request takes longer than the specified timeout.
except requests.exceptions.Timeout as errt:
print("Timeout Error:", errt)

# This is a general exception for other types of request failures not covered above.
except requests.exceptions.RequestException as err:
print("Oops: Something Else", err)

Are you also interested in SQL and Database content? Click here to check out my list on Medium.

Press enter or click to view image in full size
Photo by Nate Grant on Unsplash

Conclusion

Python’s Requests library streamlines HTTP requests, making it easier for developers to handle network communication efficiently. Its straightforward API supports a variety of tasks, from session management to JSON data handling, demonstrating its utility for data engineers and developers who frequently interact with web APIs.

Get CyCoderX’s stories in your inbox

Join Medium for free to get updates from this writer.

This guide has only scratched the surface of what’s possible with the Requests library. Beyond the basics, the library supports advanced features like streaming uploads, session objects for persisting settings across requests, and custom adapter implementations for fine-grained control over request handling.

Understanding and using the Requests library can enhance data pipelines and web service interactions, ensuring they are robust and manageable. The provided examples highlight its versatility and ease of use, solidifying its role as an essential tool in any Python developer’s toolkit.

Final Words

Thank you for taking the time to read my article.

This article was first published on medium by CyCoderX.

Hey there! I’m CyCoderX, a data engineer who loves crafting end-to-end solutions. I write articles about Python, SQL, AI, Data Engineering, lifestyle and more! Join me as we explore the exciting world of tech, data, and beyond.

Connect with me on social media:

If you enjoyed this article, consider following me for future updates.

Please consider supporting me by:

  1. Clapping 50 times for this story
  2. Leaving a comment telling me your thoughts
  3. Highlighting your favorite part of the story

--

--

CyCoderX
CyCoderX

Written by CyCoderX

Machine Learning & Data Engineer | Data Science | Python & SQL | AI | Software Developer | Azure & AWS Cloud Specialist | Blogger simplifying Big Data & Trends

Python in Plain English
Python in Plain English

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.