Python: Using the requests library

If you’re working with Python, you probably use the requests library. This library allows you to make HTTP requests in Python. This article…

Python: Using the requests library
Photo by Chris Ried on Unsplash

If you’re working with Python, you probably use the requests library. This library allows you to make HTTP requests in Python. This article will show you how to use the requests library to make HTTP requests in Python.

We’ll start with a simple example. We’ll make a GET request to the Google home page. We’ll then print out the response text:

import requests 
r = requests.get('http://www.google.com') 
print(r.text)

In this example, we imported the requests library and then made a GET request to the Google home page. We stored the response in the variable r. We then printed out the response text.

Next, we’ll show you how to make a POST request. We’ll make a POST request to the httpbin.org POST endpoint. We’ll then print out the response text:

import requests 
r = requests.post('http://httpbin.org/post', data = {'key':'value'}) 
print(r.text)

In this example, we imported the requests library and then made a POST request to the httpbin.org POST endpoint. We passed in a dictionary of data to the data parameter. We then stored the response in the variable r. We then printed out the response text.

We can also pass in headers to our request. In the following example, we’ll make a GET request to the Google home page. We’ll pass in a headers dictionary with a User-Agent key. We’ll then print out the response text:

import requests 
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'} 
r = requests.get('http://www.google.com', headers=headers) 
print(r.text)

We imported the requests library in this example and created a headers dictionary. We set the User-Agent key to a Mozilla User-Agent string. We then made a GET request to the Google home page and passed it to the headers dictionary. We stored the response in the variable r. We then printed out the response text.

We can also make requests to APIs. In the following example, we’ll make a GET request to the GitHub API. We’ll pass in a username to the API, and we’ll get back the user’s public repositories:

import requests 
r = requests.get('https://api.github.com/users/kpatronas/repos') 
print(r.text)

In this example, we imported the requests library and then made a GET request to the GitHub API. We passed in a username to the API and returned the user’s public repositories. We stored the response in the variable r. We then printed out the response text.

As you can see, the requests library is elementary to use. It allows you to make HTTP requests in Python with just a few lines of code.

This article was made with the help of New Chat (openai.com)

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…