I'm getting the following error message:
...File "c:\users\dockerhost\appdata\local\programs\python\python37\Lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I'm attempting to use requests to get the response headers. I've done a lot of research on trying to resolve the jsondecodeerror. However, I'm not finding a solution.
import requests
request.get('https://www.google.com/').json()
Error message.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Host\.virtualenvs\projects08-8iyGSYl4\lib\site-packages\requests\models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "c:\users\host\appdata\local\programs\python\python37\Lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "c:\users\host\appdata\local\programs\python\python37\Lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\users\host\appdata\local\programs\python\python37\Lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The JSON decoder expecting a value at the very first character of the very first line simply means that it is finding no content to parse. That is, the content of your response object is empty.
You should check the content of the server response with:
print(request.get(your_url).content)
to make sure that it is not empty and is actually valid JSON.
If it is indeed empty, it usually means that you are not sending to the server what it expects, and that you should fix the headers/cookies/authentication/API keys/parameters you send with the request.
From the documentation,
ResponseObj.json()
Returns a JSON object of the result (if the result was written in JSON format, if not it raises an error)
You are getting an error because of the fact that the result isn't in JSON format.
If there is a valid JSON as the result,
>>> requests.get('https://jsonplaceholder.typicode.com/todos/1').json()
{'userId': 1, 'id': 1, 'title': 'delectus aut autem', 'completed': False}
The function does return a JSON.
With the usage request.get('https://www.google.com/').json()
you are blindly converting the HTTP response content to JSON. The response may or may not be a JSON depending on the URLS. Here , google page is HTML not JSON.
From the documentation, its clear that, json()
call will raise an error, if the content is not JSON.
However, you mentioned about response headers. Response headers can be accessed with request.get('https://www.google.com/').headers
, which is a dictionary representation of headers. If you want that to be json, just load it like
import json
import requests
header_json = json.dumps(dict(requests.get('https://www.google.com/').headers))
pprint(header_json)
this would give you result like
{
"Alt-Svc": "quic=\":443\"; ma=2592000; v=\"46,43,39\"",
"Cache-Control": "private, max-age=0",
"Content-Encoding": "gzip",
"Content-Type": "text/html; charset=ISO-8859-1",
"Date": "Wed, 28 Aug 2019 06:20:28 GMT",
"Expires": "-1",
"P3P": "CP=\"This is not a P3P policy! See g.co/p3phelp for more info.\"",
"Server": "gws",
"Set-Cookie": "######################removed purposefully##########################",
"Transfer-Encoding": "chunked",
"X-Frame-Options": "SAMEORIGIN",
"X-XSS-Protection": "0"
}
Is this something you want to achieve?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With