# 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 %}

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FKU7ApQi26tcaez4qtd9H%2Fimage.png?alt=media\&token=b7fb616d-a44d-4497-81f2-48beafb2b4db)

* 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

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2F1RWQne3KTHXRtO3SeINA%2Fimage.png?alt=media\&token=c93ff8fb-c058-4690-8eb4-14d0a47cb140)

{% 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

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2Fm57QSftOba7RMC3jvrNh%2Fimage.png?alt=media\&token=7d805466-1ddd-48d8-81bf-a4d96ad0b458)

* And get proper feedback of what went wrong

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