Python - 读取 Controller 内的 header ?

标签 python odoo werkzeug request-headers odoo-10

我正在源代码控制系统和 Odoo 之间构建 Controller ,特定的集成代码源代码控制系统(如 bitbucket、github)将能够使用 json 负载数据。读取实际有效负载数据是有效的,但我正在努力的是读取 Controller 内的headers数据。

我需要 headers 数据,以便我可以识别从哪个系统接收此有效负载(例如,bitbucket 和 github 中的数据结构可能不同)。现在,如果我读取该 header ,我就会知道哪个系统有效负载数据以及如何正确解析它。

所以我的 Controller 看起来像这样:

from odoo import http
from odoo.http import request


class GitData(http.Controller):
    """Controller responsible for receiving git data."""

    @http.route(['/web/git.data'], type='json', auth="public")
    def get_git_data(self, **kwargs):
        """Get git data."""
        # How to read headers inside here??
        data = request.jsonrequest
        # do something with data
        return '{"response": "OK"}'

例如,现在我可以使用以下方式调用此路由:

import requests
import json

url = 'http://some_url/web/git.data'
headers = {
 'Accept': 'text/plain',
 'Content-Type': 'application/json',
 'type': 'bitbucket'}
data = {'some': 'thing'}

r = requests.post(url, data=json.dumps(data), headers=headers)

现在看来 Controller 会自动读取 header ,因为它知道它是 json 类型。但是,如果我需要手动检查特定 header 数据,例如 headers['type'](在我的示例中为 bitbucket),该怎么办?

我尝试查看 dir(self)dir(request),但没有看到任何与 header 相关的内容。另外 **kwargs 是空的,因此没有 header 。

注意。:request对象实际上是:

# Thread local global request object
_request_stack = werkzeug.local.LocalStack()

request = _request_stack()
"""
    A global proxy that always redirect to the current request object.
""" 
# (This is taken from Odoo 10 source)

所以基本上它是 werkzeug 的一部分。

也许有人在 werkzeugcontrollers 方面有更多的经验,所以可以为我指明正确的方向吗?

附注同样在 Odoo 本身中,我没有找到任何可以像我想要的那样读取标题的示例。看起来唯一使用 header 的地方(实际上是设置它们而不是读取)是在事后构建响应时。

最佳答案

from openerp.http import request

在您的 Controller 中处理您的特定路径。您可以使用下面的代码访问请求 header 。 (已确认 Odoo8、Odoo10...可能也适用于 Odoo9)

headers = request.httprequest.headers

关于Python - 读取 Controller 内的 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41467147/

相关文章:

python - 删除 Many2many_tags 字段的创建和编辑...

python - 获取模型中的字段值,字典样式

python - 不知道的odoo数据库注销问题

python - Flask 路由顺序很重要吗?

python - Flask:app.debug 产生回溯

python - 将环境变量从 Shippable 传递到 Docker

Python 代码在每次迭代后变慢

python - 从 for 循环到 numpy 点实现

odoo-8 - Odoo - 过滤父类中的子记录

python - 运行时错误 : working outside of application context