I need to use the below Python dictionary to be passed as a payload to REQUEST POST call
api_body_dict= {}
api_body_dict['loc1'] = {}
api_body_dict['loc1']['fName'] = var_fname1
api_body_dict['loc1']['source'] = var_source
api_body_dict['loc2'] = {}
api_body_dict['loc2']['fName'] = var_fname2
api_body_dict['loc2']['IDs'] = []
api_body_dict['loc2']['IDs'] = ID_list
api_body_dict['loc3'] = {}
api_body_dict['loc3']['fName'] = var_fname3
headers = {'Content-type': 'application/json'}
response = requests.post('https://API',json=api_body_dict, verify=False, headers=headers)
{'errors': [{'code': '5000', 'title': 'Unhandled Exception', 'detail': 'Supressed', 'status': 500, 'severity': 'FATAL', 'source': 'IndexOutOfBoundsException : Index 1 out of bounds for length 1'}]}
When passing the below JSON directly then it is running fine.
json = {"loc1": {"fName": "XYZ", "source":"db"}, "loc2": {"fName": "PQR", "IDs":["123","789"]},"loc3": {"fName":"ABC"}}
It failed when tried passing payload as api_body_dict or json.loads(json.dumps(api_body_dict))
or different combinations of this.
Any suggestions how to handle this error?
A 500 code is a Internal server Error code so the payload is incorrect or the request is incorrect.
I believe your issue is the IDs key.
api_body_dict['loc2']['IDs'] = []
It should be "groupIDs":
api_body_dict['loc2']['groupIDs'] = []
you can also write the actual dictionary out with the variables embeded.
api_body_dict = {'loc1':{'fName':var_fname1, 'source':var_source},
'loc2':{'fName':var_fname2, 'groupIDs':ID_list},
'loc3':{'fName':var_fname3}}
Hope this helped.
import requests
import json
url = "https://API"
headers = {'Content-type': 'application/json'}
payload = {'loc1':{'fName':var_fname1, 'source':var_source},
'loc2':{'fName':var_fname2, 'groupIDs':ID_list},
'loc3':{'fName':var_fname3}}
response = requests.post(url=url, payload=json.dumps(payload), headers=headers)
print(response)
Just added an example of the code I would run. just make sure that the method is correct so it is a post. also are there any required headers that are missing.