问题
I tried different ways of doing that, but they didn't work.
First I tried this way:
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"
I get the right status and the message in the client. But I get this strange warning if I do any action on the Odoo interface
Is there a way to avoid this message?
I tried this both ways as well:
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
But I always get this error as answer in the client:
TypeError: <Response 9 bytes [400 BAD REQUEST]> is not JSON serializable\n", "message": "<Response 9 bytes [400 BAD REQUEST]> is not JSON serializable"
So, is there a clean way of answer a simple message with the status of the response?
It is important for me to send the status of the response as well
If I simply respond a message everything works fine, but how to change the status without strange behaviours?
By the way, I use this script to do the calls
# -*- 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)
Update
Finally @Phillip Stack told me to do this with XML-RPC, so I wrote this other question in order to clarify my doubts.
回答1:
Try this, I am not sure if I understand all the complexities involved here. Try a vanilla request and parse the response as json as a work around. If I figure out json request/response I will update this. I was having similar issues as yourself but was able to get the following to work.
Try this for type http
@http.route('/test/test', auth='none', type='http')
def test(self, **kwargs):
return Response("TEST",content_type='text/html;charset=utf-8',status=500)
My request looks like this.
r = requests.post("http://localhost:8069/test/test",data={}))
>>> r
<Response [500]>
>>> r.text
u'TEST'
Try this for type json
@http.route('/test/test', auth='none', type='json')
def test(self, **kwargs):
Response.status = '400'
return {'test':True}
Using a request structured like this.
json_data = {"test": True}
requests.post("http://localhost:8069/test/test",data=json.dumps(json_data),headers={"Content-Type":"application/json"})
Use the above for a python request.
Use the example below for javascript
var json_data = { 'test': true };
$.ajax({
type: "POST",
url: "/test/test",
async: false,
data: JSON.stringify(json_data),
contentType: "application/json",
complete: function (data) {
console.log(data);
}
});
来源:https://stackoverflow.com/questions/39848963/how-to-send-a-simple-message-and-status-as-a-response-in-an-odoo-json-controller