v8.1.11
I keep getting this error when I try to decode a JSON object from a response body:
Traceback (most recent call last):
File "<input>", line 14, in <module>
ValueError: Unable to parse response body as JSON: com.inductiveautomation.ignition.common.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 2 path $
Here is my script:
client = system.net.httpClient()
url = "https://api.dailymotion.com/file/upload"
headers = {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br",
"Authorization": "Bearer <TOKEN>"
}
res = client.get(url, headers=headers)
print res.good # returns True
Is there any way I can fix this?
Is it actually malformed? Do you have an example of it?
Not in Postman. Here is the response:
{
"upload_url": "<url>",
"progress_url": "<url>"
}
Can you post your whole script? The error references line 14 but there’s not 14 lines in the script you posted.
It was this line (I cleaned it up when I added to post above):
print res.getJson() # res.json returns the same thing
Here is what else I tried (based on SO answers):
from com.inductiveautomation.ignition.common.gson import GsonBuilder
client = system.net.httpClient()
altGson = GsonBuilder().setLenient().create()
client.setGson(altGson)
...
Same thing happens.
Hmm, I was going to suggest basically that:
new_gson = client.getGson().newBuilder().setLenient().create()
client.setGson(new_gson)
Try printing str(res.getBody())
or something instead.
getBody()
returns data, as far as I can tell. I added the code you included above but still get the same error when I try to getJson()
.
Also, this is happening in the script console, if that matters. It wasn’t working on a button event, so I’m now investigating.
Does the above print out the JSON you’re getting or just a string rep of the bytes?
Might need something like this instead:
from java.lang import String
print String(res.getBody())
Trying to see the contents of the body without parsing it.
A bunch of gibberish (even though response says it is UTF-8 encoded).
Huh. Not sure what else to try. You probably are getting a body that isn’t JSON, but I don’t know why.
A little facepalm for future readers: there is a getText()
method on the response object.
Okay, one last suggestion: remove your gzip/deflate/br headers.
1 Like
Boom. That was it Thanks Kevin!
1 Like