Today I am going to discuss the Python requests library and its useful functions. So, let’s begin the requests
library in Python to make HTTP, requests:
Step 1: Install the requests
library (if not already installed)
pip install requests
Step 2: Import the requests
module
import requests
Step 3: Sending GET Requests
# Sending a basic GET request to a URL response = requests.get('https://api.example.com/data') # Accessing the response content print(response.text) # Checking the response status code print(response.status_code) # Accessing response headers print(response.headers)
Step 4: Sending POST Requests
# Sending a POST request with data data = {'name': 'John', 'age': 30} response = requests.post('https://api.example.com/submit', data=data) # Sending a POST request with JSON data import json data = {'name': 'John', 'age': 30} response = requests.post('https://api.example.com/submit', json=json.dumps(data)) # Accessing the response content print(response.text) # Checking the response status code print(response.status_code) # Accessing response headers print(response.headers)
Step 5: Sending Headers and Query Parameters
# Sending headers with the request headers = {'User-Agent': 'Mozilla/5.0'} response = requests.get('https://api.example.com/data', headers=headers) # Sending query parameters with the request params = {'param1': 'value1', 'param2': 'value2'} response = requests.get('https://api.example.com/data', params=params)
Step 6: Handling Errors and Exceptions
# Checking for request errors response = requests.get('https://api.example.com/data') if response.status_code == 200: print("Request succeeded") else: print("Request failed with status code:", response.status_code) # Handling exceptions try: response = requests.get('https://api.example.com/data') response.raise_for_status() print("Request succeeded") except requests.exceptions.HTTPError as err: print("HTTP Error:", err) except requests.exceptions.RequestException as err: print("Request Exception:", err)
These are some of the basic features and functionalities of the requests
library in Python. The library provides many more options and capabilities for making HTTP requests, handling authentication, handling cookies, working with sessions, and more.
Now important functions,
So, Here are some commonly used functions and features of the requests
library in Python, along with examples:
- GET Request with Query Parameters:
import requests params = {'key': 'value'} response = requests.get('https://api.example.com/data', params=params) print(response.text)
- POST Request with Form Data:
import requests data = {'username': 'john', 'password': 'secret'} response = requests.post('https://api.example.com/login', data=data) print(response.text)
- POST Request with JSON Data:
import requests import json data = {'name': 'John', 'age': 30} response = requests.post('https://api.example.com/submit', json=data) print(response.text)
- Custom Headers and Authentication:
import requests headers = {'User-Agent': 'Mozilla/5.0'} response = requests.get('https://api.example.com/data', headers=headers) auth = ('username', 'password') response = requests.get('https://api.example.com/data', auth=auth)
- Handling Response Content:
import requests response = requests.get('https://api.example.com/data') # Get response content as bytes content = response.content # Get response content as text text = response.text # Get response content as JSON json_data = response.json()
- Handling Cookies:
import requests # Send a request and receive cookies response = requests.get('https://api.example.com/login') cookies = response.cookies # Send subsequent requests with cookies response = requests.get('https://api.example.com/profile', cookies=cookies)
- Handling Timeouts:
import requests # Set a timeout for the request response = requests.get('https://api.example.com/data', timeout=5)
- Handling Redirects:
import requests # Automatically follow redirects response = requests.get('https://api.example.com/redirect', allow_redirects=True) # Disable automatic redirects response = requests.get('https://api.example.com/redirect', allow_redirects=False)
- Session Management:
import requests # Create a session session = requests.Session() # Send requests using the session response = session.get('https://api.example.com/data') # Reuse the session for subsequent requests response = session.get('https://api.example.com/profile')
These are just a few examples of the functionalities provided by the requests
library. The library offers more features such as handling SSL certificates, handling proxies, streaming responses, handling multipart/form-data, and more.
You can refer to the official requests
documentation for more information: https://docs.python-requests.org/