代码之家  ›  专栏  ›  技术社区  ›  Anders

使用中间件修改塔架中的报头

  •  1
  • Anders  · 技术社区  · 15 年前

    我试图在pylons中使用中间件修改一个报头,使我的应用程序基本上是restful的,如果用户请求 "application/json" 通过 GET 他就是这么回来的。

    我的问题是,变量 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] ?

    谨致问候,

    安德斯

    1 回复  |  直到 15 年前
        1
  •  1
  •   estin    15 年前

    试试这个

    
    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