user9074332
user9074332

Reputation: 2636

How to troubleshoot a Python Requests POST failure when the same POST is working fine in Postman?

I continue to get a very generic, unhelpful error message from python requests when making a POST request that works fine in Postman.

No matter what I try, I continue to receive one of two error messages. And note that the calling python script does not have a line 155, nor does the payload contain the letter "u":

{"error":{"detail":"SyntaxError: Unexpected token: u (sys_script_include.d2426c9ec0a8016501958bf2ac79c775.script; line 155)","message":"Unexpected token: u"},"status":"failure"}

{"error":{"message":"Unexpected token: u","detail":"SyntaxError: Unexpected token: u (sys_script_include.d2426c9ec0a8016501958bf2ac79c775.script; line 155)"},"status":"failure"}

In Postman, the parameters are correctly interpreted and then appended to the url such as:

https://areallylongurl?params={"catalogItem": "Req Name"}

In Python Requests I have tried various combinations of things without luck.

payload = {"params": '{"catalogItem": "Req Name"}'}
response = requests.post(url, headers=headers, json=payload, verify=False)
response = requests.post(url, headers=headers, json=json.dumps(payload), verify=False)
response = requests.post(url, headers=headers, data=payload, verify=False)
response = requests.post(url, headers=headers, data=json.dumps(payload), verify=False)

By using this very helpful SO answer, I was able to further analyze how the Requests library interpreted my provided payload but I am still not sure exactly how to interpret this generic error message, or what the cause may be.

Does anyone have thoughts as to what the underlying issue could be? And note that I can GET from this API without issue from Requests, it's only the POST that's problematic.

Upvotes: 2

Views: 3104

Answers (1)

blhsing
blhsing

Reputation: 107015

Since in postman the parameters are "appended to the url" like https://areallylongurl?params={"catalogItem": "Req Name"}, it means that the request is likely a GET request with JSON passed as a value to the params parameter, rather than a payload to a POST request, in which case you should do this instead:

response = requests.get(url, headers=headers, params={"params": json.dumps(payload)}, verify=False)

Upvotes: 5

Related Questions