Error in WEB API with flask

0

I started my WEB API study trying to create a Pokedex API, when I tried to create the pokemon search function by the 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)
    
asked by anonymous 12.04.2018 / 19:38

1 answer

0

Remember that json is nothing more than a string.

When you call p = pokedex() , you are setting p to what its pokedex function returns, which is a Response of the flask with data being a string in json format, not a Python dictionary .

You can iterate over a dictionary if you turn 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 to call jsonify in that dictionary within the pokedex function.

    
12.04.2018 / 20:10