Lukas_Myhan | 2021-03-16 23:01:20 UTC | #1
Hi,
I am trying to send a POST request in Python to the analytics/conversations/details/jobs endpoint. I am using a body copied directly from the API Explorer in the developer tools (and it runs with no issues there), but I am getting a 400 "malformed syntax" error when using it in this context.
I am able to get an authentication token without any issues, and if I generate the job through another process (such as through the API Explorer), I can use the jobId to get the results of the job in Python with no issues. It is only when trying to POST the job request that I am having this problem.
Here is my code:
[code=python] requestHeaders = { "Authorization": "Bearer " f"{ responsejson['accesstoken']}", 'Content-Type': 'application/json; charset=UTF-8' }
requestBody = { "interval": "2020-05-01T00:00:00.000Z/2020-05-12T00:00:00.000Z", "segmentFilters": [ { "type": "or", "predicates": [ { "dimension": "purpose", "value": "agent" }, { "metric": "tSegmentDuration", "range": { "gt": 2000, "lte": 90000 } } ] } ] }
Get roles
response = requests.post("https://api.usw2.pure.cloud/api/v2/analytics/conversations/details/jobs", data=requestBody, headers=requestHeaders)
Check response
if response.statuscode == 200: print("Got response.") else: print(f"Failure: { str(response.statuscode) } - { response.reason }") print(response.text) sys.exit(response.status_code) [/code]
And here is the result I'm getting:
[quote] Failure: 400 - Bad Request {"message":"The request could not be understood by the server due to malformed syntax.","code":"bad.request","status":400,"contextId":"028fe19f-9a13-4826-a605-9f0fab9b5db0","details":[],"errors":[]} [/quote]
Any idea what the issue is? I'm assuming it's something to do with the requestBody, but I haven't been able to figure out what it might be, and nothing I've changed seems to work.
anon28885283 | 2021-03-17 15:30:14 UTC | #2
Hi Lukas,
Upon checking and testing the code, it seems that the formed HTTP request is not sending the proper JSON. if using the requests library, you need to convert the Dictionary first to a JSON before passing it on as data:
requests.post("https://...", data=json.dumps(requestBody), headers=requestHeaders)
Alternatively, requests also has a parameter json that will take in Dictionaries, so you can use it in place of data:
requests.post("https://...", json=requestBody, headers=requestHeaders)
Either solution should work in your case.
I'd also like to recommend using the Python SDK when working with Python: https://developer.mypurecloud.com/api/rest/client-libraries/python/index.html
Lukas_Myhan | 2021-03-17 15:30:03 UTC | #3
Thank you! That did it. I have tried installing the Python SDK, but the installation keeps erroring out, and I haven't had time to resolve it. Thanks again for your help.
John_Carnell | 2021-03-25 19:15:12 UTC | #4
Hi Lukas,
I am curious what error you are getting with the Python SDK install. Would you mind posting it so we can take a look and make sure our installation process is not broken? Also, we encourage people to use the Python SDK because the Python SDK uses the urllib3 library which includes automatic retries on any 429 (e.g. rate-limiting) and respects the retry-after header on 429s. So, if you use the Python SDK, you get retry logic automatically without having to write that code yourself.
Thanks, John Carnell Manager, Developer Engagement
system | 2021-04-25 19:22:56 UTC | #5
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 10304