首页 > 解决方案 > 当 type="json" 时,Odoo 控制器避免使用 json-rpc

问题描述

我有以下路线:

@http.route([
    '/whatever/create'
], auth="none", type='json', methods=['POST'], csrf=False)

我用它来发送一个带有 json 数据的 post 请求。

有什么方法可以避免在路由上使用 json-rpc 响应type="json"?我只想回答普通的json。

如果不可能,有没有办法通过使用`type="http"来获取放在body请求上的json数据?

标签: odoo

解决方案


@http.route('/whatever/create', auth='public', methods=['POST'], type='http')
 def index(self, **kw):
    data = request.httprequest.data
    return 'success'

以上代码在 Odoo 中定义

url = "http://localhost:8069/whatever/create"

param = {
    "type_operation": "PTI",
    "label": "",
}
headers = {'Content-type': 'text/plain'}
r = requests.post(url, data=json.dumps(param), headers=headers)

以上代码我从 py 文件中请求

发送请求时,您应该更改 Content-type

'Content-type': 'application/json' --- > 'Content-type': 'text/plain'

同样在返回时只接受字符串

return {'status': 'success'} ---> return 'success'

推荐阅读