1

I cannot seem to send a POST request to a FastAPI app through Postman.

  • FastAPI version 0.89.1
  • Python version 3.10.9
from fastapi import FastAPI
from fastapi.params import Body
from pydantic import BaseModel

app = FastAPI()

class Post(BaseModel):
    title : str
    content : str

@app.get("/")
async def root():
    return {"message": "Hello."}

@app.post("/createposts/")
def create_posts(new_post:Post):
    print(new_post.title)
    return  {"new_post":f"title:"[new_post.title]}

I got the following error

INFO:     Finished server process [44982]
INFO:     Started server process [45121]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:64722 - "POST /createposts/ HTTP/1.1" 422 Unprocessable Entity

I'm following a tutorial and I cannot seem to find answers from other users.

I tried using the dict: Body(...) input argument instead.

I am also using Postman and this is the error:

{
    "detail": [
        {
            "loc": [
                "body"
            ],
            "msg": "value is not a valid dict",
            "type": "type_error.dict"
        }
    ]
}

Here's a screenshot of my request on Postman.

screenshot of Postman

I made a POST request to the URL with the POST endpoint:

{
 "title":"a",
 "content":"b"
}
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Ari
  • 23
  • 6
  • Hard coding the return statement as a dict based on the BaseModel works well. `def create_posts() -> list[Post]: return [ Post(title="A",content="B") ]` – Ari Feb 02 '23 at 21:43
  • 1
    You're not submitting a JSON body (i.e. a dictionary with the keys given in your model) in your POST request; since you haven't included the request you're sending, it's impossible to anyone to say what you're missing. – MatsLindh Feb 02 '23 at 21:44
  • Thanks, I do not understand how much more explicit I need to be when passing in my input to the function. – Ari Feb 02 '23 at 22:04
  • You can check the actual request being sent in Postman (expand the request in the console that gives you the 422) and inspect what is actually being sent to the server - make sure you're using a _JSON_ body, and not any of the other request type. Use `requests` in Python to make a more explicit request using `requests.post` for example if you want to give someone else an opportunity to see exactly what you're submitting. – MatsLindh Feb 02 '23 at 22:19
  • Please have a look at related answers [here](https://stackoverflow.com/a/71065165/17865804) and [here](https://stackoverflow.com/a/70636163/17865804) as well. – Chris Feb 03 '23 at 04:27
  • Chris, those resources were very helpful in understanding the various ways to make a POST request. Gino's answer was better suited for my inexperience in Postman, which is what I was stuck on. – Ari Feb 06 '23 at 05:34

1 Answers1

0

In Postman, you need to set the Body to be of type JSON.

In your screenshot, you have it set to Text:

screenshot of Postman, Body set to Text

It should be set to JSON:

screenshot of Postman, Body set to JSON

And it should work as expectedNote 1, as shown in the screenshot.

As MatsLindh said in the comments, you can open the panel for the Code Snippet section to check how exactly Postman converts your request:

screenshot of Postman, Code snippet shows JSON

It should show

--header 'Content-Type: application/json' 

for JSON. You probably have it as

--header 'Content-Type: text/plain'

for text, which FastAPI cannot parse properly.


Note 1
It's unrelated to the validation error, but you have a typo in the f-string on your endpoint. The closing double-quote on the value is misplaced.

This:

return  {"new_post":f"title:"[new_post.title]}

should be:

return  {"new_post": f"title:[{new_post.title}]"}
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135