3

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

enter image description here

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.

ChesuCR
  • 9,352
  • 5
  • 51
  • 114
  • what's the definition of your controller like? is it a `json` or `http` controller? what kind of requests does it accept?. we can probably guess this details but it's better you include the full code of your controller instead of `[...]` – danidee Oct 04 '16 at 12:58
  • 1
    From the looks of your external script you are trying to create a rest interface to create a contact. You should really use jsonrpc or xmlrpc as this structure already exists to do exactly what you seem to be trying to accomplish. – Phillip Stack Oct 06 '16 at 13:08
  • Thanks for your comment @phillipstack. Then, when should I use a controller? Is it needed only to show websites as a response? – ChesuCR Oct 07 '16 at 09:15
  • I wrote a [different question](http://stackoverflow.com/questions/39915198/should-i-use-xml-rpc-or-a-controller-in-odoo) to ask that – ChesuCR Oct 07 '16 at 10:58
  • I got same error like u https://stackoverflow.com/questions/58426988/got-xmlhttprequesterror-in-odoo-internal-after-return-reponse-status-400 – phoo phoo Oct 17 '19 at 07:30
  • https://stackoverflow.com/questions/58426988/got-xmlhttprequesterror-in-odoo-internal-after-return-reponse-status-400 – phoo phoo Oct 17 '19 at 07:36

1 Answers1

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);  
        } 
});
Phillip Stack
  • 3,308
  • 1
  • 15
  • 24
  • Thanks for your answer. I tried your code but I fortgot to tell you that is a json controller, so I get this error `TypeError: is not JSON serializable`. Take a look to my question again, I updated it with more lines of the controller :) – ChesuCR Oct 05 '16 at 08:01
  • I have updated my posting. Although a trivial example this has been working for me. – Phillip Stack Oct 05 '16 at 11:49
  • 1
    If I do `Response.status = '400'` I get an error in the interface as I wrote inmy question. After that I can't do any action more in Odoo because the error is always appearing – ChesuCR Oct 05 '16 at 12:15
  • What do you mean by 'you can't do any more action in odoo' I ran this with no error. I can make request and get response with no error (using terminal). – Phillip Stack Oct 05 '16 at 12:17
  • Is your odoo server logging errors? Or is your browser displaying error? Have you tried my exact example? I used this minutes ago, with no errors and I am printing info level logging. – Phillip Stack Oct 05 '16 at 12:20
  • The error appears in the browser interface. Have you tried to do any action? – ChesuCR Oct 05 '16 at 12:26
  • It is the structure of your request. I have updated my response to provide a javascript post request example. – Phillip Stack Oct 05 '16 at 13:02
  • If this does not work for you, you may want to look at default browser behaviour for a 400 status code. I am receiving no errors in the odoo logs and the browser is getting the 400 Bad Request response back with the data I responded with from the server. Which is exactly what is supposed to happen. – Phillip Stack Oct 05 '16 at 13:09
  • I don't want to get any error in the browser. What I want is to get the answer in the console in other computer with the state. Then, what do I should do? Do I understand it correctly? I updated my answer with the screenshot and I added the script that I user to do the calls – ChesuCR Oct 06 '16 at 08:47
  • i also have the same problem, when i set the response.status = 401, on the browser all subsequent calls get a 401 response and i can't do anything. looks like the Response.status = 401 assigns the status to all future responses – bermick Feb 09 '21 at 12:56