I am using a requests.get
command to retrieve data from a URL/REST endpoint. The data is supposed to be in JSON format. When I access the URL from a browser, it has the formatting you would expect, namely [{"variable1":value,"variable2":value}]
. However, when I use the requests.get
command to grab the data, the returned data is broken up and includes additional lines of values, which appear to be hexadecimals that represent the number of characters on the subsequent line. Below is an example of what response.text
returns.
2000
[{"variable1":v
2000
alue,"variable2
1ecd
":value}]
0
2000 base 16 = 8192 base 10, meaning there are 8,192 characters on the subsequent line. Note that in the above example, I purposely shortened the lines to not show 8,192 characters :).
Needless to say, this does not adhere to JSON format, so I cannot process it as expected. Any thoughts on why this may be happening? My guess was that it may have something to do with the size of the response received - len(response.content)
indicates that it is 335,898 bytes in size - so I tried chunking the response as described here, but that did not affect the output of response.text
. Appreciate any thoughts people can share :)
EDIT1: Running print(response.json())
yields the below error:
Traceback (most recent call last):
File "IncomingMessagesCompanyIDEvent.py", line 105, in <module>
data = getData()
File "IncomingMessagesCompanyIDEvent.py", line 65, in getData
text_file.write(response.json())
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\requests\models.py", line 858, in json
self.content.decode(encoding), **kwargs
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\json\decoder.py", line 342, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 6)
EDIT2: Issue resolved. See below for my answer.