콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
2571 화면

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






아바타
취소
베스트 답변

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
아바타
취소
관련 게시물 답글 화면 활동
2
7월 24
2740
1
6월 24
5193
1
10월 23
10927
1
10월 23
98
1
8월 23
2194