Odoo Help


How to create a json call from inside Odoo

Carlos Almeida
on 11/2/15, 4:18 PM 1,615 views

I need to create a method with a route like this:

@http.route('/web/test/mytest', type='json', auth="none")
def my_test(self, fields):
    do something...

    return True

How can I create another method in Odoo to be able to call it, from different odoo server, and pass some params.

Example from a server1.mydomain.com:8072 just make a json call (passing some params) to server2.mydomain:8072/web/test/mytest.

I've tried something like this, with no success:

params = {'param1': 'a', 'param2': 'b}
url = '{scheme}://{server}:{port}{path}?{params}'.format(scheme='http',
 server='localhost', port='8072', path='/web/test/mytest', params=werkzeug.url_encode(params))
headers = {'content-type': 'application/json'}
result = requests.get(url, headers=headers).json()

I got this error:

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Or you think I should use http (one of the parameter could be a big file)?

Carlos Almeida
On 11/2/15, 9:48 PM

I've reach the solution, thanks any way!

The issue was bulding the dict like {'params': {'fields': {'field1': field1}}, like this:

headers = {'content-type': 'application/json'}
params = {'fields': {'field1': field1,
'field2': field2,}}
payload = {'params': params}
response = requests.post(dest_server_url + '/web/database/mycontroller', data = json.dumps(payload), headers=headers).json()
print response['result']

This way if we have a controller like this:

 @http.route('/web/database/mycontroller', type='json', auth="none")
def mycontroller(self, fields):
print fields
return True

This will print:

{'field1': field1, 'field2': field2,}

I hope it could help someone...

Perhaps you can share your solutions, so others can learn. I am interested!

Ray Carnes
on 11/3/15, 9:29 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

4 follower(s)


Asked: 11/2/15, 4:18 PM
Seen: 1615 times
Last updated: 1/19/16, 9:43 PM