Modify headers in Pylons using Middleware
I'm trying to modify a header using Middleware in Pylons to make my application RESTful, basically, if the user request "application/json"
via GET
that is what he get back.
The question I have is, the variable headers
is basically a long list. Looking something like this:
[('Content-Type', 'text/html; charset=utf-8'), ('Pragma', 'no-cache'), ('Cache-Control', 'no-cache'), ('Content-Length','20'), ('Content-Encoding', 'gzip')]
Now, I'm looking to just modify the value based on the request - but are thes开发者_开发技巧e positions fixed? Will 'Content-Type'
always be position headers[0][0]
?
Best Regards,
Anders
Try this
from webob import Request, Response
from my_wsgi_application import App
class MyMiddleware(object):
def init(self, app):
self.app = app
def call(self, environ, start_response):
req = Request(environ)
...
rsp = req.get_response(app)
rsp.headers['Content-type'] = 'application/json'
return rsp(environ, start_response)
Or simple do request or response .headers['Content-type'] = 'application/json' in you contoller
See http://pythonpaste.org/webob/reference.html#headers
精彩评论