且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Odoo JSON控制器中发送简单的消息和状态作为响应?

更新时间:2023-11-19 19:07:40

试试这个,我不知道我是否理解这里涉及的所有复杂性。尝试一个香草请求并解析响应作为一个工作的json。如果我找出json请求/响应我会更新这个。



请尝试以下类型的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)

我的请求看起来像这样。

  r = requests.post(http:// localhost: 8069 / test / test,data = {}))
>>> r
< Response [500]>
>>>> r.text
u'TEST'

尝试类型为json

  @ http.route('/ test / test',auth ='none',type ='json')
def test self,** kwargs):
Response.status ='400'
return {'test':True}

使用这样的结构化请求。

  json_data = {test:True} 

requests.post(http:// localhost:8069 / test / test,data = json.dumps(json_data),headers = {Content-Type:application / json})

使用上面的命令获取python请求。



使用下面的示例为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);
}
});


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)

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);  
        } 
});