API WEB error with flask

Asked

Viewed 105 times

0

I started my WEB API study trying to create a Pokedex API, When I tried to create the function of searching for Pokemon by number the following error appeared:

"json.decoder.Jsondecodeerror: Expecting value: line 1 column 1 (char 0)"

appPokedex:

from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/podekex', methods=['GET'])
def pokedex():
    return jsonify({
        'pokedex':[
            {
                'numero': 1,
                'nome':'bulbasuar',
                'type':('grass','poison')
            },
            {
                'numero': 2,
                'nome':'ivysuar',
                'type':('grass','poison')
            },
            {
                'numero': 3,
                'nome':'venusuar',
                'type':('grass','poison')
            },
            {
                'numero': 4,
                'nome':'charmander',
                'type':('fire')
           },
           {
                'numero': 5,
                'nome':'charmeleon',
                'type':('fire')
           },
           {
                'numero': 6,
                'nome':'charizard',
                'type':('fire','flying')
           },
           {
                'numero': 7,
                'nome':'squirtle',
                'type':('water')
           },
           {
                'numero': 8,
                'nome':'wartortle',
                'type':('water')
           },
           {
                'numero': 9,
                'nome':'blastoise',
                'type':('water')
            },
          ]
       })

@app.route('/<int:numero>', methods=['GET'])
def getNome(numero):
    p = pokedex()
    for pokemon in p['pokedex']:
        if pokemon['numero'] == numero:
            return jsonify(pokemon)


if __name__ == '__main__':
    app.run(port=80)

clientPokedex:

import requests as req

url_pokedex = "http://127.0.0.1/podekex"

pokedex = req.api.get(url_pokedex).json()
print(pokedex)

url_busca_num = "http://127.0.0.1/1"
pokemon = req.api.get(url_busca_num).json()
print(pokemon)

ERROR:

Traceback (most recent call last):
  File "c:\Users\EmersonCarbonaro\OneDrive\Documentos\Estudo\Faculdade\terceiro_semestre\aplicações_distribuidas\aula_07\estudo\pokedex\clientPokedex.py", line 9, in <module>
    pokemon = req.api.get(url_busca_num).json()
  File "C:\Users\Emerson Carbonaro\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\models.py", line 892, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\Emerson Carbonaro\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Emerson Carbonaro\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Emerson Carbonaro\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

1 answer

0


Remember that json is nothing more than a string.

When you call p = pokedex(), is putting in p what its function pokedex returns, which is a Response flask with data being a json string, not a python dictionary.

You can iterate over a dictionary by transforming json into a dictionary:

import json
...
p = json.loads(pokedex().data)

But perhaps a better solution is to have pokedex as a dictionary in its global scope, and call jsonify in that dictionary within the function pokedex.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.