2

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.

user9074332
  • 2,336
  • 2
  • 23
  • 39
  • What exactly is producing those JSONic error messages? Is that what the response is, or is another program calling the script that uses requests? – jwodder Sep 08 '18 at 15:19

1 Answers1

5

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)
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • Wow! This fixes my problem and you have saved me countless hours of more troubleshooting so I can't thank you enough. But would you mind elaborating a little more on how you went about troubleshooting this? What tipped you off that this was the issue/fix? Also, your code snippet is using the `get` method but this same approach worked for the `post` method: `payload = {"catalogItem": "Req Name"} ` `response = requests.post(url, headers=headers, params={"params": json.dumps(payload)}, verify=False) ` – user9074332 Sep 08 '18 at 16:33
  • 2
    Glad to be of help. The URL you successfully use for postman tells everything one should know to make the request work. It's a convention of a GET request to have the query string appended to the URL in the form of `=` pairs delimited by `&`s after a question mark `?`. So it's apparent from the the URL `https://areallylongurl?params={"catalogItem": "Req Name"}` that the server is expecting a GET request with a parameter named `'params'` with a value that is JSON-formatted, so you should make such a request with `requests.get` with the `params` parameter accordingly. – blhsing Sep 09 '18 at 05:10
  • 1
    Oftentimes both GET and POST methods would work given the same API because most web app frameworks process parameters from both GET and POST requests in the same way so the web app can use consistent logics for both types of requests. – blhsing Sep 09 '18 at 05:13