【问题标题】:How to send json in response at Get Endpoint Odoo v11如何在 Get Endpoint Odoo v11 上发送 json 作为响应
【发布时间】:2018-01-11 20:42:34
【问题描述】:

目前正在尝试调用一个 odoo 控制器,它根据我的异常返回 json 数据。

@http.route('/web/update_order_webhook', type='http', csrf=False, auth="public")
def update_order_webhook(self, **kwargs):
    return Response(json.dumps({"yes":"i am json"}),content_type='application/json;charset=utf-8',status=200)

当我试图调用这个端点时

import requests

url = "http://159.89.197.219:8069/web/update_order_webhook"

headers = {
    'content-type': "application/json"
    }

response = requests.request("GET", url, headers=headers)

print(response.text)

我得到请求正文

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>Invalid JSON data: ''</p>

和请求头在我的调用端点

content-length →137
content-type →text/html
date →Thu, 11 Jan 2018 20:32:53 GMT
server →Werkzeug/0.13 Python/3.5.2

这显然意味着我没有从我的 odoo 端点获取 json 响应数据。 根据最后一个答案,我已经更新了我的代码

@http.route('/web/update_order_webhook', type='json', auth="public", website=True)
    def update_order_webhook(self, **kwargs):
         return json.dumps({"yes":"i am json"})

但现在我在调用端点时遇到了新错误

Bad Request
<function Binary.update_order_webhook at 0x7efd82ac8510>, /web/update_order_webhook: Function declared as capable of handling request of type 'json' but called with a request of type 'http'

【问题讨论】:

    标签: python json controller odoo odoo-11


    【解决方案1】:

    作为对您问题的更新,我邀请您查看下面的链接,它与您的问题相同: https://www.odoo.com/fr_FR/forum/aide-1/question/web-webclient-version-info-function-declared-as-capable-of-handling-request-of-type-json-but-called-with-a-request-of-type-http-100834

    所以解决方案是设置 python 方法,就像你已经使用类型'json'完成的那样,在请求服务器时也使用'POST 方法,在客户端你必须发出 GET 请求并获得结果来自 json 字段。

    python 方法是:

    @http.route('/web/update_order_webhook',methods=['POST'], type='json', csrf=False, auth="public")
        def update_order_webhook(self, **kwargs):
            return Response(json.dumps({"yes":"i am json"}),content_type='application/json;charset=utf-8',status=200)
    

    客户端将是:

    import requests
    url = "http://159.89.197.219:8069/web/update_order_webhook"
    payload = {'key1':'val1','key2':'val2'}
    response = requests.post(url, data=payload)
    print(response.text)
    print(response.json())
    

    查看此 url 以查看有关在 pyhton 中发出请求的新方法的更多详细信息: http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests

    更新结束

    如果要返回 HTMl 响应,则替换请求的 header 类型 :text/html,否则方法的响应类型必须为 'json'

    @http.route('/web/update_order_webhook', type='json', csrf=False, auth="public")
    def update_order_webhook(self, **kwargs):
        return Response(json.dumps({"yes":"i am json"}),content_type='application/json;charset=utf-8',status=200)
    

    还可以看看来自 odoo github repo 的这个例子: https://github.com/odoo/odoo/blob/11.0/addons/calendar/controllers/main.py

    日历主控制器的accept方法:

    @http.route('/calendar/meeting/accept', type='http', auth="calendar")
        def accept(self, db, token, action, id, **kwargs):
            registry = registry_get(db)
            with registry.cursor() as cr:
                env = Environment(cr, SUPERUSER_ID, {})
                attendee = env['calendar.attendee'].search([('access_token', '=', token), ('state', '!=', 'accepted')])
                if attendee:
                    attendee.do_accept()
            return self.view(db, token, action, id, view='form')
    

    如果你看这个方法的返回你会注意到它是一个视图(一个表单视图)所以响应类型是http

    在同一个文件中,您将找到返回 json 响应的方法 notify_ack,因此类型设置为 'json'

    @http.route('/calendar/notify_ack', type='json', auth="user")
        def notify_ack(self, type=''):
            return request.env['res.partner']._set_calendar_last_notif_ack()
    

    【讨论】:

    • 不,我只想要内容类型 Json 作为回报
    • 我更新了我的答案,检查并告诉我它是否有效。
    • 我通过在 odoo conf 文件中添加 db 过滤器解决了这个问题。因为我的服务器有多个数据库。所以请求者无法理解它必须请求哪个数据库。感谢您的努力和时间
    • 我接受你的回答,因为我有一个新问题 确认销售订单 action = { "type": "ir.actions.act_window", "view_mode": "tree", "res_model ": object.sale.order, "res_id": object.action_confirm_sale_order, }
    • 我想使用 ir.action.server 在 odoo 11 的菜单操作中添加项目,如果您知道我该怎么做,那么请更新我。我被困在那一点上。它没有显示我的菜单项。这里是官方链接,你可以理解它,然后请阅读它odoo.com/documentation/11.0/reference/actions.html#code
    【解决方案2】:

    为您的路线。使用 type="json" 标志。

    @http.route('/web/update_order_webhook', type='json', auth="public", website=True)
    def update_order_webhook(self, **kwargs):
        return json.dumps({"yes":"i am json"})
    

    【讨论】:

    • 我认为这无关紧要。
    • 好的@Phillip我会实现这个,如果它成功了,我会接受你的回答谢谢你的时间先生
    • 错误请求 , /web/update_order_webhook: 声明为能够处理“json”类型请求但使用“http”类型请求调用的函数
    • @http.route('/web/update_order_webhook', type='json', auth="public", website=True) 按照您的指示。我已经更新了我的代码。现在我得到了这个错误
    • 我认为问题出在您的请求上。
    【解决方案3】:

    是的。 ODOO HTTP GET 方法 100% 有效,请遵循我的指导。

    控制器应该是 type='json',而不是 type='http'

    “Hussain Ali”在他的 Qustation 上绝对正确,作为我在下面为 ODOO 编写的 GET 方法的解决方案

    注意:我已经通过示例代码为您提供了解决方案,您必须按照我在下面的说明更正参数。

    import requests
    
    url = "https://dev-expert.snippetbucket.com/api/get_user_information/"
    
    
    headers = {
        'content-type': "application/json",
        'user-token': "02BXD2AGqVH-TEJASTANK-gg92DwntD8f1p0tb",
        'cache-control': "no-cache",
        }
    payload = "{\"params\": {}}"
    response = requests.request("GET", url, data=payload, headers=headers)
    
    print(response.text)
    

    此解决方案绝对 100% 有效,只需添加 data=payload,这适用于 odoo,最新版本 13.0 也是如此。截至第 14 版尚未发布,所以不能说。

    解决方案也适用于 odoo 社区和企业版。

    特别说明: 后面的故事看到了,控制器中的odoo type='json'并不意味着http json请求。 type='json' 它的 json-rpc。

    问候, Tejas - SnippetBucket.com

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      相关资源
      最近更新 更多