如何使用odoofullapi在REST-get方法中返回JSON

如何使用odoofullapi在REST-get方法中返回JSON,json,python-2.7,odoo-10,webservices-client,Json,Python 2.7,Odoo 10,Webservices Client,我在odoo10中使用'GET'方法制作了一个api,我希望返回值是json。当我和邮递员一起运行下面的代码时 @http.route("/check_method_get", auth='none', type='http',method=['GET']) def check_method_get(self,**values): output = { 'results':{ 'code':200, 'message':'O

我在odoo10中使用'GET'方法制作了一个api,我希望返回值是json。当我和邮递员一起运行下面的代码时

@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)
标题中的结果是

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=/ 
在身体里的结果是

{"results": {"message": "OK", "code": 200}}
问题在于内容类型→text/html。我想要内容类型→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'
            }
        })
标题中的结果是

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}
但在人体内并没有结果。我希望
{“results”:{“message”:“OK”,“code”:200}}
在正文结果中作为json


只要我一直在“POST”方法中搜索JSON中的返回值,是否有任何线索可以解决这个问题。

我认为这个问题与处理响应上运行的Odoo有关。因为您指定了
type='http'
Odoo,所以它为一个简单的http请求添加了适当的头,而不是“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)
您的另一次尝试已将所有内容放置在标题内。您可以按如下方式修改请求

@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)

非常感谢菲利普·斯塔克先生。在跟踪和调试了odoo/odoo/http.py中的基本代码之后,我发现类响应具有构造函数init,因此我理解您的回答。我返回响应(json.dumps(body),headers=headers)。再次感谢你的帮助。哦,太好了。我将更新我的答案以反映正确的顺序。如果您认为这有帮助,请将我的答案标记为正确。谢谢你,尤尼亚