Retrieve one individual entry
We need to define a
PATH PARAMETERin the decorator
# Note that this is sigunlar to retrieve one single post
@app.get("/posts/{id}") # Has {id} because the user needs to specify the id of the post
def get_post(id):
print(id)
return {"data": f"this is the post {id}"}We can then pass the path parameter to the function directly
def get_post(id): # Note the IDThis will allow us send the HTTP Request and retrieve some info

A better way of doing this (but not best practice) would be to create a function which retrieves the posts
Then use the
PATH PARAMETERas the ID for our function
Full code:
Send the HTTP request to test the results

There is a problem with this approach. If you provide a string that cannot be converted to an integer, it will throw an INTERNAL SERVER ERROR response
We need to perform some kind of validation to ensure that whatever data is being passed to this parameter can be converted properly into an integer
This can be done directly via FastAPI
Now we can send the HTTP Request

And get proper feedback of what went wrong
Last updated