0
votes

Apologies if this is a stupid problem, but I do not have crazy amounts of experience with API's.

I have a request to do a REST API Call to post a JSON file to a endpoint via a Python script.

This is done in 2 steps:

  1. Generate OAuth token from details received (grant_type, client_id, client_secret, scope)
  2. Use this token to post the JSON structure file into the endpoint

However, I am having troubles posting the JSON file in my request.post call (I have edited the code to not show the sensitive information):

url_token = "https://login.dummyurl/oauth2/v2.0/token"
    payload = {
        'grant_type': 'client_credentials',
        'client_id' : '<Dummy_ID>',
        'client_secret' : '<Dummy_Secret>',
        'scope' : 'https:<dummyscope>.default'
    }
    headers = {'accept': "application/json", 'content-type': "application/x-www-form-urlencoded"}
    response = requests.post(url_token, data=payload, headers=headers)
    return_obj = json.loads(response.text)

This generates the token that I need, but the problem is in the following section.

I read the JSON file as follow:

f = open(file, 'r')
cust_data = json.load(f)

Then create my "header" with the auth token included and set my target URL:

hed = {'accept': 'application/json',  'Authorization': token_type + ' ' + access_token}
url_cust = 'https://api-eu.jdadelivers.com/<dummy>/<dummyChannel>/v1/<pull_data>/<endpoint>'

Then I run the POST request to post the data to the endpoint:

response_post = requests.post(url_cust, data=json.dumps(cust_data), headers =hed)
    print(response_post.json())

But I am prompted with the following error:

raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I have researched all the different methods to pull in the file differently, as it indicates that there might be a issue with the quotes etc. But I have attached a dummy sample of the JSON file I am trying to ingest.

{
    "header": {
        "sender": "SENDER",
        "receiver": "RECEUVER",
        "model": "MODEL",
        "messageVersion": "VERSION",
        "messageId": "DUMMY_ID",
        "type": "ENDPOINT_DUMMY",
        "creationDateAndTime": "2020-07-20T11:30:49.115+05:30"
    },
    "demandChannel": [
        {
            "creationDateTime": "2020-07-20T11:30:49.579+05:30",
            "documentStatusCode": "STATUS",
            "documentActionCode": "CODE",
            "demandChannelId": "1",
            "description": "DUMMY_TEST",
            "componentDelimiter": "-",
            "avpList": [
              {
                "CUSTOMERLVL_3": "TEST",
                "CUSTOMERLVL_3_DESCR": "",
                "CUSTOMERLVL_2": "TEST",
                "CUSTOMERLVL_2_DESCR": "",
                "CUSTOMERLVL_1": "TEST",
                "ATTRIBUTE_1": "01",
                "ATTRIBUTE_2": "TEST",
                "ATTRIBUTE_3": "TEST",
                "ATTRIBUTE_4": "TEST",
                "ATTRIBUTE_5": "TEST",
                "ATTRIBUTE_6": "TEST",
                "ATTRIBUTE_7": "TEST",
                "ATTRIBUTE_8": "TEST",
                "ATTRIBUTE_9": "TEST",
                "ATTRIBUTE_10": "TEST",
                "ATTRIBUTE_11": "",
                "ATTRIBUTE_12": "",
                "ATTRIBUTE_13": "",
                "ATTRIBUTE_14": "X",
                "ATTRIBUTE_15": ""
              }
              ]

        }
 ]
}

Your help is greatly appreciated.

1
It is complaining that what you are receiving from the server is not a json, so when you call response_post.json() it gives an error. Try printing the reponse, maybe it will give you an error message - Blupper

1 Answers

0
votes

I found the problem - because of the custom API, I had to state the in the header dictionary the "User-Agent" parameter, and then posted successfully.