Search code examples
python-3.xpython-requestsurllib

Python3 Request from API return Not Found


I am trying to get a balance from some address at a explorer on Python but I get 404 error and I believe that is related to a list.

This is my code:

import urllib.request, json, requests

listAddress = [
    "HiaRFZkLiWaUuu2x3Dxg2rAMu6BTrzoitf"
]
explorerUrl = 'http://explorer.htmlcoin.com'
urlBase = '/api/tokens/Hj5mkJmDWKq8HAh9qDYLnbohZHw6kaUG3A/addresses/'
balance = '/balance'


for address in listAddress:
    urlGamb = explorerUrl+urlBase+address+balance
    json_data = urlGamb
    r = requests.post(json_data)
    r.status_code
    r.json()
    print(r.json())

The problem is that returns this error:

python3 newwork.py 
{'status': 404, 'url': '/api/tokens/Hj5mkJmDWKq8HAh9qDYLnbohZHw6kaUG3A/addresses/HiaRFZkLiWaUuu2x3Dxg2rAMu6BTrzoitf/balance', 'error': 'Not found'}

So, what am I doing wrong here?


Solution

  • Minor suggestion:

    urlGamb = f"{explorerUrl}{urlBase}{address}{balance}"
    

    I don't know about the API, you did not provide a link, but the value of urlGamb is 'http://explorer.htmlcoin.com/api/tokens/Hj5mkJmDWKq8HAh9qDYLnbohZHw6kaUG3A/addresses/HiaRFZkLiWaUuu2x3Dxg2rAMu6BTrzoitf/balance' and that is not JSON. The assignment to json_data seems like a conceptual error because the URL string is not json. If the post requires a JSON payload then the docs should tell you how to construct it.

    Should the post have been a get request instead? That would make sense because according to REST standards, post is for changing state on the server, and you are only making a query.