小编Aem*_*zai的帖子

如何在odoo 14中使用json类型设置路由中的响应状态代码

我在 odoo 14 中创建了一条 Json 类型的路线。

@http.route('/test', auth='public', methods=['POST'], type="json", csrf=False)
def recieve_data(self, **kw):
       headers = request.httprequest.headers
       args = request.httprequest.args
       data = request.jsonrequest
Run Code Online (Sandbox Code Playgroud)

因此,当我使用此路由收到请求时,一切都很好,但假设我想返回不同的状态代码(如 401),我无法使用类型为 json 的路由来执行此操作。我也尝试过以下方法,但此方法的问题是它会停止所有 odoo 未来的请求。

from odoo.http import request, Response
@http.route('/test', auth='public', methods=['POST'], type="json", csrf=False)
def recieve_data(self, **kw):
      headers = request.httprequest.headers
      args = request.httprequest.args 
      data = request.jsonrequest
      Response.status = "401 unauthorized"
      return {'error' : 'You are not allowed to access the resource'}
Run Code Online (Sandbox Code Playgroud)

因此,在上面的示例中,如果我将响应状态代码设置为 401 ,则所有其他请求(即使它们发送到不同的路由)也将被停止,并且其状态代码更改为 401 。我在odoo14和odoo13中都检查过这个问题。

response httpresponse json-rpc python-3.x odoo

4
推荐指数
1
解决办法
3233
查看次数

标签 统计

httpresponse ×1

json-rpc ×1

odoo ×1

python-3.x ×1

response ×1