question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to post dictionary data type in FastAPI Request Forms ?

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
upeshjindalcommented, Feb 15, 2022

@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

0reactions
growwithsundarcommented, Jun 13, 2022

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found