views:

216

answers:

2

How can I include an HTTP header, such as Cache-Control or Last-Modified, in the response to a django-piston call?

A: 

Not sure about django-piston, but in django you can just go:

from django.http import HttpResponse
response = HttpResponse('My content')
response['MyHttpHeader'] = 'MyHeaderValue'

So, do that where you can get access to the response. Middleware is often the perfect place to do this if you are using a 3rd party application. Your middleware could look something like:

def process_response(self, request, response):
    response['MyHttpHeader'] = 'MyHeaderValue'
    return response
Humphrey
A: 

You can wrap it in your urls.py following the procedure in the specifying per view cache in urlconf guide in the Django documentation. In my case I had my Piston API in a separate module and prefer to use Varnish instead of the built-in Django caching framework so I used this approach in my api/urls.py (which my main urls.py includes) to set the cache control headers I wanted:

from django.views.decorators.cache import cache_control

cached_resource = cache_control(public=True, maxage=30, s_maxage=300)

urlpatterns = patterns('',
   url(r'^myresource/$', cached_resource(Resource(MyHandler))),
)
Chris Adams