Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
2531 Lượt xem

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






Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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
Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 7 24
2686
1
thg 6 24
5148
1
thg 10 23
10865
1
thg 10 23
98
1
thg 8 23
2193