# Retrieve one individual entry

* We need to define a `PATH PARAMETER` in the decorator

```python
# 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

```python
def get_post(id): # Note the ID
```

* This will allow us send the HTTP Request and retrieve some info

{% hint style="info" %}
Note: We have a hardcoded post with ID 2
{% endhint %}

![](/files/wseSBeei6vKq1HnG0jV1)

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

```python
# Function that retrieves the post based on id
def find_post(id):
    for p in my_posts:
        if p["id"] == id:
            return p
```

* Then use the `PATH PARAMETER` as the ID for our function

```python
@app.get("/posts/{id}") # Has {id} because the user needs to specify the id of the post
def get_post(id):
    post = find_post(id)

    return {"post_details": post}
```

Full code:

```python
from fastapi import FastAPI
from typing import Optional
from pydantic import BaseModel
from random import randrange

app = FastAPI()

class Post(BaseModel):
    title: str
    content: str
    published: bool = True
    rating: Optional[int] = None

# Global variable to save the post in memory
my_posts = [

    {
    "title": "title of post 1",
    "content": "content of post 1",
    "id": 1
    },

    {
    "title": "favorite foods",
    "content": "Pizza",
    "id": 2
    }
]

# Function that retrieves the post based on id
def find_post(id):
    for p in my_posts:
        if p["id"] == id:
            return p
            
            
@app.get("/posts/{id}") # Has {id} because the user needs to specify the id of the post
def get_post(id):

# Note that we need the integer ID here
    post = find_post(int(id))

    return {"post_details": post}
```

* Send the HTTP request to test the results

![](/files/GCAQJiBYXXpdzKAmwWVj)

{% hint style="danger" %}
There is a problem with this approach. If you provide a string that cannot be converted to an integer, it will throw an <mark style="color:red;">`INTERNAL SERVER ERROR`</mark> response&#x20;
{% endhint %}

* 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

```python
# Old validation code from above:
@app.get("/posts/{id}")
    post = find_post(int(id))

    return {"post_details": post}
    
# Validating directly with FastAPI
@app.get("/posts/{id}")
def get_post(id: int):   # Here is where we validate
    post = find_post(id) # We no longer need to convert

    return {"post_details": post}
```

* Now we can send the HTTP Request

![](/files/8pJUjv3ZKMH96STZlGZy)

* And get proper feedback of what went wrong

```json
{
    "detail": [
        {
            "loc": [
                "path",
                "id"
            ],
            "msg": "value is not a valid integer",
            "type": "type_error.integer"
        }
    ]
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.arkannis.net/programming/python/frameworks/fastapi/retrieve-one-individual-entry.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
