How to use Dynamic Form Data in FastAPI application function Python?
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.
- 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:
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:
- Created 2 years ago
- Comments:5
Top 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 >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
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.
So then I cannot parse the form dynamically…So can I close this issue/question then??..Or you could go ahead and close it