The request could not be understood by the server due to malformed syntax When doing POST API call

I am getting this error:
{'message': 'The request could not be understood by the server due to malformed syntax.', 'code': 'bad.request', 'status': 400, 'contextId': 'e1afaf83-0d02-4ff6-94ff-d56fc94e6154', 'details': [], 'errors': []}

while trying to access this endpoint:
POST /api/v2/analytics/users/aggregates/query

request_headers = {
'Authorization': "Bearer " + token,
'Content-Type': "application/json"
}
request_body = {
"interval": "2022-08-22T04:00:00.000Z/2022-08-23T04:00:00.000Z",
"groupBy": [
"queueId"
],
"metrics": [
"nOffered"
]
}

When I use the developer tool, I can see the response, even when I do get, I get the desired result

response = requests.get("https://api.cac1.pure.cloud/api/v2/oauth/clients",headers=request_headers)

This doesnt work

response = requests.post("https://api.cac1.pure.cloud/api/v2/analytics/conversations/aggregates/query",data=request_body,headers=request_headers)

Really unsure what is the issue here,

Thanks in advance

I don't know what's wrong with your request exactly, but the HTTP payload you're sending is malformed in some way. My guess is that your code is sending request_body as a stringified python object or maybe form data and isn't serializing it to JSON first. The specific error for that correlation ID is "Error code [bad.request] type [JsonParseException] message [Unrecognized token 'interval': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')]"

You could also try using the Python SDK: https://developer.genesys.cloud/devapps/sdk/docexplorer/purecloudpython/AnalyticsApi#post_analytics_conversations_aggregates_query

1 Like

[Solution]
Thanks!! @tim.smith
I found the solution for my issue..

I was sending request_body as a data
response = requests.post("https://api.cac1.pure.cloud/api/v2/analytics/conversations/aggregates/query",data=request_body,headers=request_headers)

When I tried sending request_body as JSON ,

response = requests.post("https://api.cac1.pure.cloud/api/v2/analytics/conversations/aggregates/query",json=request_body,headers=request_headers)

I got the response I was looking for!!

Posting the solution here for future

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.