This question has been flagged
1 Reply
9892 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)?

Avatar
Discard
Author Best Answer

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...

Avatar
Discard

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