Home > Mobile >  Handling variable Path Parameter in Python FastApi
Handling variable Path Parameter in Python FastApi

Time:04-19

I'm trying to write a simple get request for my Angular Frontend in FastApi

i've created this endpoint with the parameter of the item_id:

@app.get("/pokemon/{item_id}")
async def getPokemon(item_id: int):
    response = pokemon.getPokemon()
    return response

and in the getPokemon() i go to the official Api and make the get Request:

def getPokemon():
    response = requests.get('https://pokeapi.co/api/v2/pokemon/{item_id}'),
    pokemonOutput = json.loads(response.text)
    return pokemonOutput

My Question is, if i make the request to my endpoint and send the item_id parameter from the frontend with it. How can i make it so the item_id is passed as variable in the url of the get Request to the official API?

I can't seem to find anything by googling. Thx for helping!

CodePudding user response:

you just modify the function

def get_pokemon(item_id):
    response = requests.get('https://pokeapi.co/api/v2/pokemon/' item_id),
    pokemonOutput = json.loads(response.text)
    return pokemonOutput

and call it from you're endpoint

  • Related