skip to Main Content

In browser I got this. I’m trying to get json but got error:

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Code:

import requests
url = 'https://api-mainnet.magiceden.io/idxv2/getBidsByMintAddresses?hideExpired=true&mintAddresses=GXXBt4tzJ6aGoNvz87Xzh1PqXwB4qVRdQdHcX65iXtRZ&direction=1&field=2&limit=500&offset=0'
response = requests.get(url)
data = json.loads(response.text)
print(data)

3

Answers


  1. Use builtin json function of the response

    Also best practice to check status codes

    response = requests.get(url)
    if response.status_code == 200:
      data = response.json()
      print(data)
    else:
      print("unexpected response")
    
    Login or Signup to reply.
  2. Note this:

    data = json.loads(response.text)
    

    Response has a built-in method for this purpose. It’s called as the json()

    data = resp.Json()
    

    Additionally, I would preface this with a conditional if check:

    if resp.Ok:
        ...
    else:
        print('NOK!')
        r.raise_for_status()
    
    Login or Signup to reply.
  3. My personal preference would be:

    import requests
    
    URL = 'whatever'
    
    with requests.get(URL) as response:
        response.raise_for_status()
        print(response.json()
    

    raise_for_status() will raise requests.exceptions.HTTPError if the status code indicates some kind of error. Clearly, you might want to wrap that in try/except

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search