Storing in Array

  • You could store the info in a Global variable in Memory to test out the functionality

my_posts = [

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

    {
    "title": "favorite foods",
    "content": "Pizza",
    "id": 2
    }
]
  • This creates 2 posts with IDs so we can test out our HTTP Requests

  • We have the following python function to retrieve the data

@app.get("/posts")
def get_posts():
    return {"data": my_posts}
  • If we use a GET request via Postman to get the data

  • We will get the following data

{
    "data": [
        {
            "title": "title of post 1",
            "content": "content of post 1",
            "id": 1
        },
        {
            "title": "favorite foods",
            "content": "Pizza",
            "id": 2
        }
    ]
}
  • We get the data properties followed by the posts that we have in the List/Array

Last updated