Python’s Request Lib — How to Use it
One of the Most Used Python’s Lib
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 requestsNow 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
# 200You can also get the response’s headers, JSON, cookies, etc…
response.headers
response.json()
response.cookiesAuthentication
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 requestssession = 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:




