avatarEsteban Thilliez

Summary

The provided web content is an informative article about the Python Requests library, detailing its usage for making HTTP requests, handling responses, and managing sessions.

Abstract

The article "Python’s Request Lib — How to Use it" is a guide that emphasizes the importance and utility of the Requests library in Python for performing HTTP operations. It explains the installation process via pip, the types of requests such as GET, POST, PUT, DELETE, HEAD, and PATCH, and how to send them with URL parameters and headers. The article also covers the Response object, which encapsulates the server's response, providing access to the text, status code, headers, JSON, and cookies. Authentication methods, including the use of the "Authorization" header and HTTPBasicAuth, are discussed to access protected resources. Additionally, the article introduces the concept of sessions for persisting data and cookies across multiple requests, and concludes by advocating for the library's power and centrality to many other Python libraries.

Opinions

  • The author suggests that the Requests library is one of the most used and useful libraries in Python, implying its widespread adoption and necessity for developers working with HTTP requests.
  • The article implies that understanding the Requests library is fundamental for tasks such as web scraping, interacting with APIs, and building web services.
  • The author's inclusion of examples for each type of request indicates a pedagogical approach, aiming to provide clear and practical instructions for learners.
  • By mentioning that many Python libraries are based on or wrap around the Requests library, the author conveys its foundational role in the Python ecosystem.
  • The encouragement to explore more Python libraries and the invitation to follow the author's content suggest that the author values community engagement and continuous learning within the developer community.

Python’s Request Lib — How to Use it

One of the Most Used Python’s Lib

Photo by Ashwini Chaudhary(Monty) on Unsplash

This article is part of the Python Libraries Series. Find more below!

Requests is one of the most used, and one of the most useful libraries in Python. It allows you to easily make HTTP requests to URLs.

Usually, it’s used when you want to get data from APIs, scrape the web, or even build your own APIs with Python.

Python requests provide functions to manipulate easily HTTP requests and responses.

Send Requests

The first thing to do is to install requests. The easiest way to do it is by using pip.

pip instal requests

Now we can start making requests. There are several types of requests:

  • GET: get data from a server.
  • POST: send data to a server.
  • PUT: send data to be stored to a server.
  • DELETE: delete the specified resource.
  • HEAD: same as GET, but the response has no body.
  • PATCH: used to modify resources.

The format to send requests is requests.METHOD(url, ..., **kwargs)

For example, we can send a GET request to Google:

url = "https://www.google.com"
response = requests.get(url)

The response will be the source code of the page.

We can also send data to Google using a POST request:

response = requests.post(url, data={"name": "John", "age": 30})

In addition, we can send headers through requests:

response = requests.put(url, data={"name": "John", "age": 30}, headers={"X-Header": "value"})

Response Object

When sending requests, you receive a response every time. The response is wrapped into a Response object to make our lives easier.

For example, you can get the response’s text, or the status code, using attributes:

url = "https://www.google.com"
response = requests.get(url)

text = response.text
status_code = response.status_code
print(text)
print(status_code)
# Google's source code
# 200

You can also get the response’s headers, JSON, cookies, etc…

response.headers
response.json()
response.cookies

Authentication

With requests, you have many ways to authenticate and access restricted resources.

A way to do this is through the “Authorization” header:

url = "https://www.google.com"

auth_token = "1234567890"
headers = {"Authorization": f"Bearer {auth_token}"}

response = requests.get(url, headers=headers)

Obviously, it makes no sense to try to authenticate this way on Google, but it’s just to show you how it works.

Depending on your needs, you may also require the HTTP authentication:

import requests
from requests.auth import HTTPBasicAuth


if __name__ == "__main__":
    url = "https://www.google.com"
    response = requests.get(url, auth=HTTPBasicAuth("username", "password"))

Sessions

If you want to persist data and cookies between requests, you can use a Session object.

import requests
session = requests.Session()

It works exactly the same way as when using requests (except cookies and data persist between requests):

session = requests.Session()

url = "https://www.google.com"
response = session.get(url)

Final Note

As you can see, Python’s request is a powerful library allowing you to easily send HTTP requests. Many Python libraries are based on requests lib, and most of those that adapt APIs to make them easy to use in Python are simply wrappers for this lib. That’s why I think it’s useful to know this lib!

If you want to discover other Python libraries, click below!

If you liked the story, don’t forget to clap and maybe follow me if you want to explore more of my content :)

You can also subscribe to me via email to be notified every time I publish a new story, just click here!

If you’re not subscribed to medium yet and wish to support me or get access to all my stories, you can use my link:

Python
Programming
Coding
Learning To Code
Web Development
Recommended from ReadMedium