【发布时间】:2016-10-04 09:38:46
【问题描述】:
我尝试了不同的方法,但没有奏效。
首先我是这样尝试的:
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': 'jamon@test.com',
}
}
data_json = json.dumps(data_res_partner)
response = requests.post(url=url, data=data_json, headers=headers)
print(response.status_code)
print(response.content)
更新
最后@Phillip Stack 告诉我用 XML-RPC 来做这件事,所以我写了这个other question 来澄清我的疑惑。
【问题讨论】:
-
你的控制器的定义是怎样的?是
json还是http控制器?它接受什么样的请求?我们可能会猜到这些细节,但最好包含控制器的完整代码,而不是[...] -
从您的外部脚本的外观来看,您正在尝试创建一个休息界面来创建一个联系人。您真的应该使用 jsonrpc 或 xmlrpc,因为这种结构已经存在,可以完全完成您似乎想要完成的工作。
-
感谢您的评论@phillipstack。那么,什么时候应该使用控制器呢?是否只需要显示网站作为响应?
-
我写了一个different question来问这个问题
-
我遇到了和你一样的错误stackoverflow.com/questions/58426988/…
标签: json controller response odoo-8 odoo