Skip to content
Advertisement

Cant decode HTTP Response to JSON (Python3)

I am running a series of API tests and this one is giving me an error. As far as I can tell response.read() converts the HTTP response into binary and then decode() converts that binary into a string but it’s an empty string. When I copy paste the binary string into Python3 (b'{"error": {"code": "INVALID_TOKEN", "description": ""}}') and issue a decode() command it works fine. I can even import json and do a loads() call on that string.

Can anyone suggest why this is happening?

JavaScript

Error:

JavaScript

Advertisement

Answer

The response object is a “file-like” object. You are trying to read the response twice, but at the second call you are getting an empty string.

You may want to change your code to something along the lines of:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement