Skip to Content
Menu
This question has been flagged
1 Reply
2213 Views

I want to send json data to Odoo:


ODOO_ENDPOINT = "http://12.34.567.890:12345/odoo_api_ep"
API_HEADERS = {'Content-type': 'application/json', 'Accept': 'text/plain'}


some_data = [
{
"A": "John",
"B": 1000
},
{
"A": "Jane",
"B": 500
}
]
some_data = json.dumps(some_data)

    r = requests.get(ODOO_ENDPOINT, headers=HEADERS, data=some_data)

Inside odoo I have:

class IncomingAPI(http.Controller):
@http.route('/odoo_api_ep', type='json')
def incoming_data_api(self, context=None):
my_data = request.jsonrequest
return my_data


I get 500 Internal Server Error.

When I don't send a json string (without json.dumps()), I get 400 Bad Request, invalid JSON data.

Thank you for any suggestions






Avatar
Discard
Best Answer

1) You can only pass JSON data and not a list/array in the request. You can pass the data like this:

some_data = {'data': [
{
"A": "John",
"B": 1000
},
{
"A": "Jane",
"B": 500
}
]}

2) Your Odoo route should have authentication "public/none":
auth='none' or auth='public'

@http.route('/odoo_api_ep', type='json', auth='none')

Modify your code as per the suggestion and it will work.

SCRIPT:

import json
import requests
ODOO_ENDPOINT = "http://0.0.0.0:8069/odoo_api_ep"
API_HEADERS = {'Content-type': 'application/json', 'Accept': 'text/plain'}
some_data = {'data': [
{
"A": "John",
"B": 1000
},
{
"A": "Jane",
"B": 500
}
]}
some_data = json.dumps(some_data)
r = requests.get(ODOO_ENDPOINT, headers=API_HEADERS, data=some_data)
print("rrrr... ", r.text)

Odoo Route:

@http.route('/odoo_api_ep', type='json', auth='none')
def incoming_data_api(self, context=None):
my_data = request.jsonrequest
print("\n\nmy_data..... ", my_data)
return my_data
Avatar
Discard
Related Posts Replies Views Activity
2
Jul 24
1727
1
Jun 24
4332
1
Oct 23
9821
1
Oct 23
98
1
Aug 23
2193