I am tying to utilize the system.net.httpClient() to send a request to an API. The API is setup to use a PUT for us to retrieve data based on an accompanying JSON payload.
Unfortunately my code only ever returns a Server Error (500) and the exception java.lang.NullPointerException.
Below is the function I've written to interface with the API.
def getQADAPIDataForRun(url, payload):
apiParams = payload
apiRunData = None #init API run data
qadresponse = None
qadURL = url
try:
qadClient = system.net.httpClient()
qadresponse = qadClient.put(url=qadURL, data=apiParams)
if qadresponse.good:
apiRunData = qadresponse.json['items']
runDataValid = True
elif qadresponse.clientError:
runDataValid = False
msg = 'Client Error Occured: {}.'.format(qadresponse.clientError)
print(msg)
elif qadresponse.serverError:
runDataValid = False
msg = 'Server Error Occured: {}.'.format(qadresponse.serverError)
print(msg)
else:
runDataValid = False
msg = 'OTHER Error Occured.'
print(msg)
except Exception as e:
#raise an exception
runDataValid = False
msg = 'An Exception Occured: {}'.format(str(e))
print(msg)
return runDataValid, apiRunData, msg, responseRequest
The above is being called from the Script Console.
I can successfully get a response from the API using Postman's PUT method to the URL, providing the raw JSON string.
I would have thought the team creating the API would have developed a GET method since we are getting data from the API. My knowledge of Web stuff is rudimentary at best, so maybe my understanding of GET/PUT methods are incorrect. Will the httpClient PUT method work in this case? Is there anything glaringly wrong with my function?
The URL being passed in to the function is the same as used in my Postman test, so I am confident in it. The "easy to read" payload being provided to the function is as follows:
{
"request": {
"ds_rundata": {
"RunInfo": [
{
"RI_domain": "US1",
"RI_run_id": "R1000907"
}
]
}
}
}
The actual payload string is,
{"request":{"ds_rundata":{"RunInfo":[{"RI_domain":"US1","RI_run_id":"R1000907"}]}}}"
The API development team is using Insomnia to test their API, and it gets the same results as my test using Postman. So that indicates there is something wrong with the content I've developed. As noted, my less than stellar Web knowledge is likely what is preventing me from seeing my mistake(s), so any help would be appreciated.