Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine - headers[] and headers.add_header() for cache control

What is the proper way to set cache control?

Sometimes I see the use of headers[]

self.response.headers["Pragma"]="no-cache"
self.response.headers["Cache-Control"]="no-cache, no-store, must-revalidate, pre-check=0, post-check=0"
self.response.headers["Expires"]="Thu, 01 Dec 1994 16:00:00"

Other times, I see headers.add_header()

self.response.headers.add_header("Pragma","no-cache")
self.response.headers.add_header("Cache-Control","no-cache, no-store, must-revalidate, pre-check=0, post-check=0")
self.response.headers.add_header("Expires","Thu, 01 Dec 1994 16:00:00")

And even a mix of both headers[] and headers.add_header()

self.response.headers["Pragma"]="no-cache"
self.response.headers.add_header("Cache-Control","no-cache, no-store, must-revalidate, pre-check=0, post-check=0")
self.response.headers.add_header("Expires","Thu, 01 Dec 1994 16:00:00")
like image 424
user365918 Avatar asked Jun 25 '10 00:06

user365918


People also ask

How do I add HTTP header in Python?

To add HTTP headers to a request, you can simply pass them in a dict to the headers parameter. Similarly, you can also send your own cookies to a server using a dict passed to the cookies parameter.

What are request headers?

A request header is an HTTP header that can be used in an HTTP request to provide information about the request context, so that the server can tailor the response. For example, the Accept-* headers indicate the allowed and preferred formats of the response.

How does Google handle multiple requests?

Handling requests Any request can be routed to any instance, so consecutive requests from the same user are not necessarily sent to the same instance. An instance can handle multiple requests concurrently. The number of instances can be adjusted automatically as traffic changes.


1 Answers

The difference is that using headers[] will overwrite previous values, while add_header won't.

From the wsgiref.headers docs (referred to by the GAE docs), "Setting a header deletes any existing values for that header, then adds a new value at the end of the wrapped header list."

like image 147
Matthew Flaschen Avatar answered Oct 14 '22 22:10

Matthew Flaschen