Preventing Caching with HTTP Headers [Python Code]

To prevent caching, the client must send the correct minimum set of three HTTP headers that work on all servers and proxies (Cache-Control, Pragma, and Expires). The Cache-Control header conforms to the HTTP 1.1 specification for clients and proxies. The Pragma header conforms to the HTTP 1.0 specification for legacy clients. The Expires header conforms to the HTTP 1.0 and 1.1 specifications for clients and proxies. In this HTTP Headers to Prevent Caching example, we send a request to a ReqBin echo URL. Click Send to execute the HTTP Headers to Prevent Caching example online and see the results. The Python code was automatically generated for the HTTP Headers Prevent Caching example.
Preventing Caching with HTTP Headers [Python Code] Send
GET /echo HTTP/1.1
Host: reqbin.com
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

Updated: Viewed: 19957 times
Python code for HTTP Headers Prevent Caching example

Python code for HTTP Headers Prevent Caching Example

This Python code snippet was generated automatically for the HTTP Headers Prevent Caching example.
<< Back to the HTTP Headers Prevent Caching example

What are HTTP Headers?

The HTTP headers enable clients to send additional information to the server and enable the server to provide additional information to clients. The HTTP header is invisible to the end-user and can only be seen by clients, servers, and network administrators. The purpose of Custom HTTP headers is to provide additional information related to the current request or response to help with troubleshooting. HTTP headers contain a case-insensitive name followed by a colon ":" and its value. Spaces before the value are ignored.

What is Cache-Control?

The Cache-control is an HTTP header specifying the caching policies for client requests and server responses. Caching policies specify how the resource is cached, where it is cached, and how long it can be cached before it expires.

What is Pragma?

The Pragma is a non-cache header in HTTP/1.0 that is intended to be used in the request-response chain. A pragma header is meant to prevent the client from caching the response, which indicates the browser wants a fresh version from the server.

What is Expires?

The Expires HTTP header specifies the date/time after which a response is considered expired. Usually, the lifetime of a resource is set via the Expires header, but if you need to be more specific, you can set the "max-age" parameter in seconds. And this directive takes precedence over Expires, but it's always a good idea to set this header for backward compatibility. Invalid expiration dates with a value of 0 represent a date in the past and mean that the resource is already expired.

HTTP Headers to Prevent Caching Syntax

The following is the correct minimum set of headers to prevent caching:

HTTP Headers to Prevent Caching Syntax
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

How to prevent caching with HTTP headers?

The following is an example of preventing caching with HTTP headers:

HTTP Headers to Prevent Caching Example
GET /echo HTTP/1.1
Host: reqbin.com
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

Server response to our prevent caching request:

Example
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Cache-Control: private, max-age=3600, must-revalidate
Expires: date

See also

Generate code snippets for Python and other programming languages

Convert your HTTP Headers Prevent Caching request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the Python code generator.