When making HTTP requests in Python using the requests library, response caching is enabled by default. This means that requests caches responses on the filesystem and will return cached responses instead of making actual network requests if the same request is made multiple times.
Caching can be useful for improving performance, but sometimes you want to explicitly disable caching, especially during development/testing. Here are a few ways to make Python
Set Cache Control Headers
You can disable caching by setting cache control request headers before making the request:
import requests
url = 'https://api.example.com/data'
headers = {'Cache-Control': 'no-cache'}
response = requests.get(url, headers=headers)
This will send cache control headers with the request to indicate the response should not be cached.
Use Session and Set Cache Disabled
Another option is to create a
session = requests.Session()
session.cache_disabled = True
response = session.get(url)
This will ensure no caching for all requests made with that session.
Use Cache Busting Parameters
You can also append a unique parameter like a timestamp to the URL on each request to avoid getting cached responses:
import time
url = f'https://api.example.com/data?cachebuster={time.time()}'
response = requests.get(url)
The changing parameter prevents caching and forces a new request.
So in summary, Python