hux0
hux0

Reputation: 207

ERROR: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I wanted to get some data via API from this url https://api.hooktheory.com/v1/users/auth

I can enter it with a key, but as soon as I want to request some data it won't work, because of the following error.

I already downloaded pip via https://bootstrap.pypa.io/get-pip.py and imported requests, I also tried it via conda install pip. Nothing works out and the problem still occurs. I already searched for some solutions in here, but its. not a duplicate. FYI: I work on a Mac OS X with visual studio.

import requests
import time

login = {"Accept": "application/json", 
      "Content-Type": "application/json",
      "username":"huks",
      "password": "XXXX"}


url = "https://api.hooktheory.com/v1/users/auth"
r = requests.post(url, data=login)
print(r.json())

time.sleep(5)

activkey = 'XXXX'
header = {"Authorization": "Bearer " + activkey}

r = requests.get(url+'trends/songs', headers=header)
r.json()

r = requests.get(url+'trends/nodes?cp=4', headers=header)
r.json()

Here is the traceback + error message:

File "/Users/marius/Desktop/INNOLAB/tempCodeRunnerFile.py", line 20, in <module>
    r.json()
  File "/Users/marius/anaconda3/lib/python3.7/site-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/Users/marius/anaconda3/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/Users/marius/anaconda3/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/marius/anaconda3/lib/python3.7/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)

Upvotes: 3

Views: 4287

Answers (1)

Kris
Kris

Reputation: 8873

import requests
import time

login = {"Accept": "application/json", 
      "Content-Type": "application/json",
      "username":"huks",
      "password": "XXXX"}


url = "https://api.hooktheory.com/v1/users/auth"
r = requests.post(url, data=login)
print(r.json())

time.sleep(5)

activkey = 'XXXX'
header = {"Authorization": "Bearer " + activkey}

r = requests.get(url+'/trends/songs', headers=header)
r.json()

r = requests.get(url+'/trends/nodes?cp=4', headers=header)
print(r.text) #this will print what is the response you got!
if r.status_code == 200:
    print(r.json()) #this will work only if response is JSON

Hope the comments makes sense!

Upvotes: 1

Related Questions