我试图在pylons中使用中间件修改一个报头,使我的应用程序基本上是restful的,如果用户请求 "application/json" 通过 GET 他就是这么回来的。
"application/json"
GET
我的问题是,变量 headers 基本上是一个很长的列表。看起来像这样:
headers
[('Content-Type', 'text/html; charset=utf-8'), ('Pragma', 'no-cache'), ('Cache-Control', 'no-cache'), ('Content-Length','20'), ('Content-Encoding', 'gzip')]
现在,我只想根据请求修改值-但是这些位置是固定的吗?威尔 'Content-Type' 始终保持姿势 headers[0][0] ?
'Content-Type'
headers[0][0]
谨致问候,
安德斯
试试这个
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)
或简单的do请求或响应。contoller中的headers['content-type']='application/json'
见 http://pythonpaste.org/webob/reference.html#headers