0

I'm a newbie in python and I'm dealing with this problem, I want to make an API POST request by using Python but I can't send a request with this. Can anyone help me figure it out? This is what I have:

import requests

headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
data = 
{
    "token": "TokenStaging",
    "OrderCode": "23FNQY46"
}
r = requests.post('https://dev-online-gateway.ghn.vn/apiv3-api/api/v1/apiv3/OrderInfo',
    headers=headers, data=data)

API document link: https://api.ghn.vn/home/docs/detail?id=29#parameter-examples-APIV3-OrderInfo-1_0_0-0

aitorhh
  • 2,331
  • 1
  • 23
  • 35
Tan Huynh
  • 105
  • 11
  • 1
    Does this answer your question? [Post JSON using Python Requests](https://stackoverflow.com/questions/9733638/post-json-using-python-requests) – farincz May 25 '20 at 21:30
  • Your `headers` are a set instead of a dict - format it the same way as `data`. – Luka Mesaric May 25 '20 at 21:33
  • There's an issue with the headers object. You misquoted the strings, so it's a set instead of a dict. Use this: `headers = {'Accept': application/json', 'Content-Type': 'application/json'}`. – Milan Cermak May 25 '20 at 21:33
  • @farincz I tried but get the 400 ERROR CODE :( – Tan Huynh May 25 '20 at 21:49
  • I tried all of you guys solution but still get that 400 error code – Tan Huynh May 25 '20 at 21:52
  • There's something wrong with the endpoint. I get `{"code":0,"msg":"An error occured","data":{"decode":"invalid character \'o\' in literal true (expecting \'r\')"}}` in response. Changing `"token":` to `"taken":` in `data` produces `{"code":0,"msg":"An error occured","data":{"decode":"invalid character \'a\' in literal true (expecting \'r\')"}}` in response. The endpoint isn't parsing `data` correctly. – Michael Ruth May 25 '20 at 22:09
  • Do token and OrderCode parameters have valid values? Try to print some additional info from the response to start debugging it This one could help you get more detail **print(r.text)** – Wellington Costa May 25 '20 at 23:37

1 Answers1

0

After some research and testing, i finally can call that API with this :

import requests

r = requests.post('https://console.ghn.vn/api/v1/apiv3/OrderInfo',
 json={
  "token": "TokenStaging",
  "OrderCode": "23FNQY46"
})

Thank you guys for all your support

Tan Huynh
  • 105
  • 11