1
@app.post("/posts")
def post_req(payload: dict = Body(...)):
    print(payload)
    return {"Message": "Posted!!!"}

I am using the above path operation function to receive POST requests, but when I am trying to make a request using Postman, it says value is not a valid dict.

In Postman I am sending the below in the request body:

{
    "title" : "This is title"
}

The response I get in Postman is as follows:

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

VS Code terminal (server side) is showing this:

127.0.0.1:51397 - "POST /posts HTTP/1.1" 422 Unprocessable Entity
Chris
  • 18,724
  • 6
  • 46
  • 80
BumbleBee
  • 37
  • 1
  • 7
  • Does this answer your question? [How to post JSON data from JavaScript frontend to FastAPI backend?](https://stackoverflow.com/questions/73759718/how-to-post-json-data-from-javascript-frontend-to-fastapi-backend) – Chris Sep 30 '22 at 04:56
  • I went through it and the Option 2 is working for me when I am using str instead of dict could you please tell me why dict is showing me an error. – BumbleBee Sep 30 '22 at 05:35
  • 1
    Please make sure you are posting the request in the right way through Postman. Have a look at [this answer](https://stackoverflow.com/a/71065165/17865804) and [this answer](https://stackoverflow.com/a/71489056/17865804). When using `payload: dict = Body(...)`, FastAPI will expect a body like:`{"some key": "some value"}`. – Chris Sep 30 '22 at 05:42
  • Got it through these answers thanks. – BumbleBee Sep 30 '22 at 06:08

3 Answers3

3

When defining your payload Body parameter like this:

payload: dict = Body(...)

and is the only Body parameter defined in your endpoint, FastAPI will expect a body like:

{
  "some key": "some value"
}

Since you have a single Body parameter, you could also use the special Body parameter embed:

payload: dict = Body(..., embed=True)

in which case, FastAPI would expect a body like:

{
  "payload": {"some key": "some value"}
}

Please have a look at this answer, as well as this answer and this answer for more details.

When sending the request through Postman

Also, the 422 Unprocessable Entity error shows that the body received doesn't match the expected format. Hence, please make sure you are posting the request body through Postman in the right way. That is, go to Body -> raw, and select JSON from the dropdown list to indicate the format of your data. Please take a look at the answers here and here for more details.

Chris
  • 18,724
  • 6
  • 46
  • 80
1

You need to do:

{
    "payload": {"title": "This is title"}
}
Tom McLean
  • 5,583
  • 1
  • 11
  • 36
1

Select data type that you are sending as json in postman. This will 100% resolve your error.

Amit gupta
  • 11
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – user11717481 Oct 20 '22 at 10:42