0

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!

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

.. and you'd call it by passing the item_id in your view function: response = pokemon.getPokemon(item_id). (another small note to OP: You'd usually use get_pokemon or just pokemon as the method name in Python, not camelcased like getPokemon (as in JS or Java).
yes my bad for the method name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.