开发者

Using django-piston, how can I write out HTTP headers in the response?

开发者 https://www.devze.com 2022-12-24 20:39 出处:网络
How can I include an HTTP header, such as Cache-Contr开发者_StackOverflow社区ol or Last-Modified, in the response to a django-piston call?You can wrap it in your urls.py following the procedure in the

How can I include an HTTP header, such as Cache-Contr开发者_StackOverflow社区ol or Last-Modified, in the response to a django-piston call?


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))),
)


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号