我在 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中都检查过这个问题。