Requests is a popular third-party library in Python for making HTTP requests. It abstracts the complexities of making requests behind a simple API, allowing you to send HTTP/1.1 requests. With it, you can add content like headers, query parameters, and form data via simple Python libraries to HTTP requests.
In this article, we will cover:
Before you can start using Requests, you need to install it. You can install the library using the following command:
pip install requests
Once the installation is complete, you can start using the library in your Python code.
To make a request, you can use the requests.get()
or requests.post()
functions, depending on the type of request you want to make.
A GET request retrieves data from a specified resource. Here’s a simple example of making a GET request to a URL:
import requests
response = requests.get('https://api.example.com/data')
print(response.text)
A POST request submits data to a specified resource to be processed. Here’s an example of making a POST request with some data:
import requests
data = {'name': 'John', 'age': 30}
response = requests.post('https://api.example.com/submit', data=data)
print(response.text)
Query parameters are key-value pairs that you can include in the URL of a GET request. To add query parameters to a request, you can use the params
keyword argument:
import requests
payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.get('https://api.example.com/data', params=payload)
print(response.url)
print(response.text)
To upload a file, you can use the files
keyword argument:
import requests
files = {'file': open('example.txt', 'rb')}
response = requests.post('https://api.example.com/upload', files=files)
print(response.text)
To download a file, you can use the iter_content()
method to read the content in chunks:
import requests
url = 'https://example.com/example.zip'
response = requests.get(url, stream=True)
with open('example.zip', 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
To handle timeouts, you can use the timeout
parameter:
import requests
try:
response = requests.get('https://api.example.com/data', timeout=2)
print(response.text)
except requests.Timeout:
print('The request timed out')
To handle other errors, you can use the raise_for_status()
method:
import requests
response = requests.get('https://api.example.com/data')
try:
response.raise_for_status()
except requests.HTTPError as error:
print(f'An HTTP error occurred: {error}')
Requests supports several authentication schemes, including Basic, Digest, and OAuth. Here’s an example of using Basic authentication:
import requests
from requests.auth import HTTPBasicAuth
response = requests.get('https://api.example.com/secure', auth=HTTPBasicAuth('username', 'password'))
print(response.text)
By default, Requests will automatically handle redirects. You can use the history
attribute to see the redirect chain:
import requests
response = requests.get('https://api.example.com/redirect')
print(response.url)
print(response.history)
To disable automatic redirects, you can use the allow_redirects
parameter:
import requests
response = requests.get('https://api.example.com/redirect', allow_redirects=False)
print(response.status_code)
print(response.headers['Location'])
In this article, we covered the basics of using the Requests library in Python. With this knowledge, you should be able to make HTTP requests, handle query parameters, upload and download files, manage timeouts and errors, authenticate requests, and handle redirects.
The Requests library offers many more features and options, so be sure to check out its official documentation for more information.