Skip to content

How to post dictionary data type in FastAPI Request Forms ? #4555

@srcodesrepo

Description

@srcodesrepo

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

@menu.post("/add-menu")
async def addmenu(
    ItemName: str = Form(...),
    ItemShortDesc: str = Form(...),
    ItemPrice: str = Form(...),
    ItemCategory: list = Form(...),
    ItemStatus: str = Form(...),
    ItemChoices: list[dict[str,str]] = Form(...)):
    
    return ItemChoices

Description

POST Method using FastAPI Request Form
Request Body | application/x-www-form-urlencoded

I am trying to do,

  1. "list" of "dict" using request form - error Code:422
  2. Only "dict" using request form - error Code:422
  3. without "dict" - Its Worked

I got the below error, error Code:422

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

How can I use "dict" or "list" of "dict" in FastAPI request form? Please help me out of any possible chance.

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.73.0

Python Version

python3.9

Additional Context

Screenshot 2022-02-12 at 8 55 06 AM

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions