如何 return JSON 在 REST get 方法中使用 Odoo full API

How to return JSON in REST get method using the Odoo full API

我在 odoo10 中使用 'GET' 方法创建了一个 api,我希望 return 值在 json 中。当我 运行 我的代码在 postman

下面
@http.route("/check_method_get", auth='none', type='http',method=['GET'])
def check_method_get(self,**values):
    output = {
        'results':{
            'code':200,
            'message':'OK'
        }
    }

    return json.dumps(output)

Headers 中的结果是

Content-Length →43
Content-Type →text/html; charset=utf-8
Date →Mon, 30 Apr 2018 15:07:30 GMT
Server →Werkzeug/0.11.11 Python/2.7.12
Set-Cookie →session_id=505500f3f5b83ada1608d84e38d2f1776006b443;  Expires=Sun, 29-Jul-2018 15:07:30 GMT; Max-Age=7776000; Path=/ 

Body 中的结果是

{"results": {"message": "OK", "code": 200}}

问题是Content-Type→text/html。我想要 Content-Type →application/json。然后我在下面更改我的代码

@http.route("/check_method_get", auth='none', type='http',method=['GET'])
def check_method_get(self,**values):
    return Response(headers={
            'Content-Type': 'application/json',
            'results':{
                'code':200,
                'message':'OK'
            }
        })

Header中的结果是

Content-Length →0
Content-Type →application/json
Date →Mon, 30 Apr 2018 15:18:41 GMT
Server →Werkzeug/0.11.11 Python/2.7.12
Set-Cookie →session_id=505500f3f5b83ada1608d84e38d2f1776006b443;   Expires=Sun, 29-Jul-2018 15:18:41 GMT; Max-Age=7776000; Path=/
results →{'message': 'OK', 'code': 200}

但是Body没有结果。我希望 Body 中的 {"results": {"message": "OK", "code": 200}} 结果为 json.

有没有解决问题的线索,只要我一直在'POST'方法中搜索JSON中的return值。

我认为这个问题与 Odoo 在响应中 运行 的处理有关。因为您指定 type='http' Odoo 会为简单的 http 请求添加适当的 header 而不是 'application/json'.

试试这个

@http.route("/check_method_get", auth='none', type='json',method=['GET'])
def check_method_get(self,**values):
    output = {
        'results':{
            'code':200,
            'message':'OK'
        }
    }
return json.dumps(output)

您的其他尝试已将所有内容放入 header。您可以按如下方式修改请求使其工作。

@http.route("/check_method_get", auth='none', type='http',method=['GET'])
def check_method_get(self,**values):
    headers = {'Content-Type': 'application/json'}
    body = { 'results': { 'code':200, 'message':'OK' } }

    return Response(json.dumps(body), headers=headers)
import json
from odoo import http
from odoo.http import request, Response, JsonRequest

from odoo.tools import date_utils


class OdooAPI(http.Controller):
    def alternative_json_response(self, result=None, error=None):

        if error is not None:
            response = error

        if result is not None:
            response = result

        mime = 'application/json'

        body = json.dumps(response, default=date_utils.json_default)

        return Response(

            body, status=error and error.pop('http_status', 200) or 200,

            headers=[('Content-Type', mime), ('Content-Length', len(body))]

        )

    # ...

    @http.route('/api/auth/', type="json", auth="none", methods=["POST"], csrf=False)
    def get_session(self, *args, **kwargs):
        # your auth code here
        data = {
            "result": "data info",
            "status": 200,
            "info": "success"
        }
        request._json_response = self.alternative_json_response.__get__(request, JsonRequest)
        return data