# Send Data via Body of HTTP Request

We are using Postman to create the HTTP Request with Body data

{% hint style="info" %}
For additional info on how Postman works [click here](https://docs.arkannis.net/programming/postman)
{% endhint %}

* Go to the Body of the POST request

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FYP4gZgrIh46qCzWQiCcM%2Fimage.png?alt=media\&token=861d7272-794b-44bc-9028-a65d80a0d0f2)

* Go to RAW under the Body section and usually you want to select JSON

{% hint style="info" %}
Note: You can use other types as well depending on needs. Such as XML, JS, Text
{% endhint %}

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FJ2fzA9J2eYqJH2yC1ngE%2Fimage.png?alt=media\&token=c1b945da-1f2b-47ba-809d-d601bcb7c923)

{% hint style="info" %}
JSON works very similar to a python dictionary data structure
{% endhint %}

How it would look:

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2F1mNfO2lSwLs3LBpVg6ae%2Fimage.png?alt=media\&token=0db22250-e105-43ef-9ce4-549f1dcdd12a)

```json
{
    
    "title": "top gun", 
    "content": "check out my top gun"

}
```

#### Now that we have our HTTP packet set up, we can move on to the FastAPI code

* You will have to import the <mark style="color:green;">Body</mark> parameter from <mark style="color:purple;">`fastapi.params`</mark>

```python
from fastapi.params import Body
```

* Once this is imported you are able to set up the function so that it converts the JSON sent via HTTP request to a python dictionary and stores it into the <mark style="color:purple;">`payload`</mark> variable

```python
def create_posts(payload: dict = Body(...)):
```

* Then you can return the variable

```python
def create_posts(payload: dict = Body(...)):
    return payload
```

Full Code:

```python
from fastapi.params import Body

@app.post("/createpost")
def create_posts(payload: dict = Body(...)):
    return payload
```

#### Alternatively you can print this to the screen:

```python
@app.post("/createpost")
def create_posts(payload: dict = Body(...)):

    print(payload)

    return {"message": "successfully created post"}

```

Once you run this via uvicorn and send the packet via postman you should get the dictionary printed in the terminal:

* Note on line 8 that the dictionary got printed

```bash
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [29399]
INFO:     Started server process [29594]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
{'title': 'top gun', 'content': 'check out my top gun'}
INFO:     127.0.0.1:37532 -"POST /createpost HTTP/1.1" 200 OK
```

#### Even better you could return the results via an f string referencing the dictionary key

```python
@app.post("/createpost")
def create_posts(payload: dict = Body(...)):

    print(payload)

    return {"new_post": f"title: {payload['title']} content: {payload['content']}"}
```

* Note that the <mark style="color:purple;">`payload`</mark> dictionary can be referenced normally by key

Postman Results:

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FLUdCWv5fNIH0mTmQN462%2Fimage.png?alt=media\&token=dce91bbc-fc00-4262-8466-29f2f479a19a)
