Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to load a random dad joke form this website and but there is an error with json.loads

here is the full error message for when i run the code that is under this error message:

PS C:\Users\Admin> & C:/Users/Admin/AppData/Local/Programs/Python/Python39/python.exe c:/Users/Admin/Desktop/dadjoke.py
Traceback (most recent call last):
  File "c:\Users\Admin\Desktop\dadjoke.py", line 9, in <module>
    data = json.loads(str(question.text ))
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads    
    return _default_decoder.decode(s)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode    
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\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)
PS C:\Users\Admin> 

here is my code:

import requests
import json

params = {"q":"joke"}
url = "https://icanhazdadjoke.com"
question = requests.get(url, params)

if question.status_code == 200:
  data = json.loads(str(question.text ))
  print(data)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I’ve just take a look at the API docs of the website.
Accept Header

Since you didn’t specify your Accept header, the website will return a HTML response as default

Solution:

headers = {
  "Accept": "application/json"
}
url = "https://icanhazdadjoke.com"

r = requests.get(url, headers = headers)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading