Retrieve one individual entry

  • We need to define a PATH PARAMETER in 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 ID
  • This will allow us send the HTTP Request and retrieve some info

Note: We have a hardcoded post with ID 2

  • A better way of doing this (but not best practice) would be to create a function which retrieves the posts

  • Then use the PATH PARAMETER as the ID for our function

Full code:

  • Send the HTTP request to test the results

  • 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