How to post dictionary data type in FastAPI Request Forms ?
See original GitHub issueFirst 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,
- “list” of “dict” using request form - error Code:422
- Only “dict” using request form - error Code:422
- 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
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
Form Data - FastAPI
Data from forms is normally encoded using the "media type" application/x-www-form-urlencoded . But when the form includes files, it is encoded as ...
Read more >How to post dictionary data type in FastAPI Request Forms ...
My POST using FastAPI Request Form: Request Body | application/x-www-form-urlencoded. I am trying to post,. "list" of "dict" using request ...
Read more >Requests - Starlette
The request body, parsed as form data or multipart: await request.form() ... receive, send): assert scope['type'] == 'http' request = Request(scope, ...
Read more >Scripting API: Networking.UnityWebRequest.Post - Unity
The Content-Type header will be set to application/x-www-form-urlencoded . The Dictionary of strings in formFields will be interpreted as a list of form...
Read more >Basics of Building a CRUD API with Flask or FASTApi
create a get route return a dictionary with a property that is an tuple of dictionary versions of the BlogPost objects, we'll use...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@growwithsundar You cannot send the data as a json in form fields. It needs to be sent as a stringified JSON and on the FastAPI side, you need convert this to your desired object.
Form fields work okay for simple datatypes.
See the link for reference: https://stackoverflow.com/questions/60127234/how-to-use-a-pydantic-model-with-form-data-in-fastapi
Thanks it worked