The Python Requests library offers various tricks and tips for making HTTP requests. Here are some examples of its usage:
- Sending HTTP requests with arbitrary data:
The requests
library allows you to send HTTP requests with arbitrary data, such as JSON, XML, or even binary data. Here’s an example of sending a JSON payload to a server:
import json
import requests
data = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('https://example.com/api', json=data)
print(response.json())
- Setting custom headers:
You can set custom headers for your HTTP requests using the headers
parameter of the request()
function. Here’s an example of setting a custom Authorization
header:
import requests
response = requests.get('https://example.com/api', headers={'Authorization': 'Bearer my_token'})
print(response.json())
- Sending files:
You can send files using the files
parameter of the request()
function. Here’s an example of sending a file to a server:
import requests
file = open('file.txt', 'rb')
response = requests.post('https://example.com/api', files={'file': file})
print(response.json())
- Sending multipart data:
You can send multipart data using the multipart
parameter of the request()
function. Here’s an example of sending a multipart form:
import requests
data = {'key1': 'value1', 'key2': 'value2'}
files = {'file': open('file.txt', 'rb')}
response = requests.post('https://example.com/api', data=data, files=files)
print(response.json())
- Setting the User-Agent:
You can set the User-Agent header using the headers
parameter of the request()
function. Here’s an example of setting the User-Agent to a custom value:
import requests
response = requests.get('https://example.com/api', headers={'User-Agent': 'My Custom User-Agent'})
print(response.json())
- Following redirects:
By default, the requests
library follows redirects. You can disable this behavior by setting the allow_redirects
parameter to False
. Here’s an example of disabling redirects:
import requests
response = requests.get('https://example.com/api', allow_redirects=False)
print(response.status_code)
- Using proxies:
You can use a proxy server to make requests using the proxies
parameter of the request()
function. Here’s an example of using a proxy server:
import requests
proxies = {'https://example.com': 'http://proxy.example.com'}
response = requests.get('https://example.com/api', proxies=proxies)
print(response.json())
- Setting the timeout:
You can set the timeout for a request using the timeout
parameter of the request()
function. Here’s an example of setting a timeout of 10 seconds:
import requests
response = requests.get('https://example.com/api', timeout=10)
print(response.json())
- Using the
async
parameter:
You can use the async
parameter of the request()
function to make asynchronous requests. Here’s an example of making an asynchronous GET request:
import requests
async def fetch_data():
response = requests.get('https://example.com/api', async=True)
return response.json()
data = fetch_data()
print(data)
- Using the
Session
class:
The Session
class is a convenient way to make multiple requests to the same server without having to specify the URL and headers for each request. Here’s an example of using a Session
object to make multiple requests:
import requests
s = requests.Session()
response1 = s.get('https://example.com/api/1')
response2 = s.get('https://example.com/api/2')
print(response1.json())
print(response2.json())
- Custom Headers:
You can add custom headers to your HTTP requests using theheaders
parameter. For example:
import requests
headers = {
'User-Agent': 'Your User Agent'
}
response = requests.get('https://www.example.com', headers=headers)
- Timeouts:
Setting a timeout for your request can be done as follows:
import requests
response = requests.get('https://www.example.com', timeout=5)
- Retries:
You can set the number of retries for your request in case of a connection error:
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
s = requests.Session()
retries = Retry(total=5, backoff_factor=1, status_forcelist=[ 500, 502, 503, 504 ])
s.mount('https://', HTTPAdapter(max_retries=retries))
These are just a few examples of the many features and tricks available in the requests
library. With a little creativity and experimentation, you can use requests
to perform a wide range of HTTP tasks.
These examples demonstrate some of the powerful features of the Requests library, such as custom headers, timeouts, and retries, which can be used to make robust and reliable HTTP requests
Thanks for your time
Jesus Saves
By Jesse JCharis