【问题标题】:Modify headers in Pylons using Middleware使用中间件修改 Pylons 中的标头
【发布时间】:2011-02-15 20:24:35
【问题描述】:

我正在尝试使用 Pylons 中的中间件修改标头以使我的应用程序 RESTful,基本上,如果用户通过 GET 请求 "application/json",这就是他得到的结果。

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

最好的问候,

安德斯

【问题讨论】:

    标签: python rest pylons middleware


    【解决方案1】:

    试试这个

    
    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)

    或者简单地在你的控制器中请求或响应 .headers['Content-type'] = 'application/json'

    http://pythonpaste.org/webob/reference.html#headers

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      相关资源
      最近更新 更多