1

目前正在尝试调用一个 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'
4

3 回答 3

2

作为对您问题的更新,我邀请您查看下面的链接,它与您的问题相同: 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 响应,则替换请求的标头类型 :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()
于 2018-01-11T23:31:00.267 回答
0

是的。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

于 2020-02-19T11:30:17.917 回答
0

为您的路线。使用 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"})
于 2018-01-11T20:50:30.323 回答