我尝试了不同的方法来做到这一点,但它们不起作用。
首先我尝试了这种方式:
import openerp.http as http
from openerp.http import Response
class ResPartnerController(http.Controller):
@http.route('/odoo/create_partner', type='json', auth='none')
def index(self, **kwargs):
Response.status = '400'
return "Result message"
我在客户端中获得了正确的状态和消息。但如果我在 Odoo 界面上执行任何操作,我会收到这个奇怪的警告
有没有办法避免这个消息?
我也尝试过这两种方法:
data = {'result': 'RESULT message'}
json_data = json.dumps(data, encoding='utf-8')
headers = [('Content-Type', '{}; charset=utf-8'.format('application/json'))]
mimetype = 'application/json'
res = Response(
response=json_data,
status=status,
headers=headers,
mimetype=mimetype,
)
return res
msg = u'Response 200 badly built, falling back to a simple 200 OK response'
res = Response(msg, status=200)
return res
但我总是在客户端中收到此错误作为答案:
TypeError: <Response 9 bytes [400 BAD REQUEST]> is not JSON serializable\n", "message": "<Response 9 bytes [400 BAD REQUEST]> is not JSON serializable"
那么,是否有一种干净的方法来回答带有响应状态的简单消息?
发送回复状态对我来说也很重要
如果我只是回复一条消息,一切都会正常,但是如何在不出现奇怪行为的情况下更改状态?
顺便说一下,我使用这个脚本来进行调用
# -*- coding: utf-8 -*-
import requests
import json
url = 'http://localhost:8069/odoo/create_partner'
headers = {'Content-Type': 'application/json'}
data_res_partner = {
'params': {
'name': 'Example',
'email': '[email protected]',
}
}
data_json = json.dumps(data_res_partner)
response = requests.post(url=url, data=data_json, headers=headers)
print(response.status_code)
print(response.content)
Update
最后 @Phillip Stack 告诉我用 XML-RPC 来做到这一点,所以我写了这个其他问题为了澄清我的疑惑。