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:
- Installing Requests
- Making GET and POST requests
- Handling query parameters
- Uploading and downloading files
- Handling timeouts and errors
- Authenticating requests
- Handling redirects and following links in web pages
1. Installing Requests
Section titled “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 requestsOnce the installation is complete, you can start using the library in your Python code.
2. Making GET and POST requests
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “4. Uploading and downloading files”Uploading Files
Section titled “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
Section titled “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
Section titled “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
Section titled “6. Authenticating requests”Requests supports several authentication schemes, including Basic, Digest, and OAuth. Here’s an example of using Basic authentication:
import requestsfrom requests.auth import HTTPBasicAuth
response = requests.get('https://api.example.com/secure', auth=HTTPBasicAuth('username', 'password'))print(response.text)7. Handling redirects and following links in web pages
Section titled “7. Handling redirects and following links in web pages”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
Section titled “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.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js