David's Blog

Using Requests in Python A Comprehensive Guide

By David Li on 2025-01-15T01:07:52.000Z

Using Requests in Python: A Comprehensive Guide

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:

  1. Installing Requests
  2. Making GET and POST requests
  3. Handling query parameters
  4. Uploading and downloading files
  5. Handling timeouts and errors
  6. Authenticating requests
  7. Handling redirects and following links in web pages

1. Installing Requests

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.

2. Making GET and POST requests

To make a request, you can use the requests.get() or requests.post() functions, depending on the type of request you want to make.

GET Request

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)

POST Request

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)

3. Handling query parameters

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)

4. Uploading and downloading files

Uploading Files

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)

Downloading Files

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)

5. Handling timeouts and errors

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}')

6. Authenticating requests

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'])

Conclusion

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.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-11-21.