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 use Dynamic Form Data in FastAPI application function Python?

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.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Example

Here’s a self-contained, minimal, reproducible, example with my use case:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.post("/do_something")
def do_something(data:str = Form(...)): # Here this data: str=Form(...)should be dynamic.....taking data from HTML form
   print(data)
   return ""

Description

Normally If we have form data like:

<form action="/do_something" method="post">
   <label>SomeLabel</label>
   <input type="text" name="data">
   <input type="submit" value="Submit">
</form>

We can use something like this in FastAPI application

@app.post("/do_something")
def do_something(data:str = Form(...)):
   print(data)
   return ""

But when the HTML changes according to inputs like:

<form action="/do_something" method="post">
   {%for i in some_list%}
   <label>{{i}}</label>
   <input type="text" name="{{i}}">
   {%endfor%}
   <input type="submit" value="Submit">
</form>

So then what do we do for taking all these inputs as parameters for the function. Also the /docs of the FastAPI application should contain these things as text fields. Something like this: Take a look at what it should look like on /docs

Please Help Fast… Thanks

Also the known solution is something around using request and request.form()

@app.post("/do_something")
def do_something(request: Request):
   data = request.form()
   print(data)
   return ""

But in this approach nothing shows up on /docs endpoint. I want the data to show up on the swagger docs also

Environment

  • OS: [e.g. Linux / Windows / macOS]: macOS
  • FastAPI Version [e.g. 0.3.0]: 0.65.2
  • Python version: 3.8.10

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
boanergiescommented, Jul 19, 2021

I’m just a fellow user who is answering questions because I’m waiting for my own to be answered.

Whether to close the issue is up to you. If you feel your question has been answered, then close the issue, otherwise you can wait to see if someone else gives a different answer.

0reactions
RakshithRAcharyacommented, Jul 19, 2021

When I think about this more its not realistic to expect a ‘List’ of Forms.

Rather it would be important for the forms your building to extract what is constant and what is dynamic and have the route work with the data as it is updated.

Like if someone wanted a list of aliases along with a username that could be updated

@app.post("/do_something")
def do_something(username: str = Form(...), aliases: List[str] = Form(...)):

If your use case is literally having an entire Form be dynamic you may have to parse the request directly like your potential solution shows. The idea of Forms from what I understand is its not expected the entire data to be parsed can not be known before doing so.

So then I cannot parse the form dynamically…So can I close this issue/question then??..Or you could go ahead and close it

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Dynamic Form Data in FastAPI application ...
We can use something like this in FastAPI application @app.post("/do_something") def do_something(data:str = Form(...)): print(data) return ...
Read more >
Form Data - FastAPI
To use forms, first install python-multipart . ... Import Form from fastapi : from fastapi import FastAPI, Form app = FastAPI() @app.post("/login/") async ......
Read more >
Post arbitrary JSON data (dynamic form) to FastAPI using AJAX
Firstly, we create a function to convert the form data to be indexed: Then in the function of posting data, we create a...
Read more >
FastAPI Handle Form Data & Upload Files - YouTube
In this video, we will take a look at handling Forms and Files from a client request. This requires a python -multipart to...
Read more >
How to parse the request data using Form and still log the ...
I used the GitHub search to find a similar issue and didn't find it. I searched the FastAPI documentation, with the integrated search....
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