-
First Check
Commit to Help
Example Codefrom fastapi import Request, FastAPI, Form
app = FastAPI()
@app.post("/post_name")
async def parse_data(request: Request, first_name: str = Form(...), last_name: str = Form(...)):
data_body = await request.body()
logging.info(data_body)
logging.info(first_name)
logging.info(last_name)
return first_name + last_nameDescriptionI have an endpoint This can be simulated using a curl command: However, occasionally I may receive people who accidentally send their data using For requests which are successful, I am able to log the The above code snippet should do this, but gives me the following error whenever I receive a request: What am I doing wrong when trying to access Thank you. Operating SystemLinux Operating System DetailsNo response FastAPI Version0.68.1 Python VersionPython 3.8.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
|
Use the from fastapi import Request, FastAPI, Form
app = FastAPI()
@app.post("/post_name")
async def parse_data(request: Request, first_name: str = Form(...), last_name: str = Form(...)):
data_body = await request.form()
logging.info(data_body)
logging.info(first_name)
logging.info(last_name)
return first_name + last_name |
Beta Was this translation helpful? Give feedback.
-
|
@frankie567 This solution works if a user passes in the correct parameters e.g. However, if the user fails to pass in all of the correct parameters e.g. The following curl command demonstrates this behavior with your provided code: How can I capture the form payload even if I get a |
Beta Was this translation helpful? Give feedback.
-
|
Yes, that's how FastAPI works: if you define it as a required parameter in your endpoint, FastAPI will trigger its validation mechanism. If you need something completely dynamic, you'll have to remove those parameters and perform the validation yourself: from fastapi import Request, FastAPI, Form, HTTPException
app = FastAPI()
@app.post("/post_name")
async def parse_data(request: Request):
data_body = await request.form()
logging.info(data_body)
if "first_name" not in data_body or "last_name" not in data_body:
raise HTTPException(422, "Missing fields")
return first_name + last_name |
Beta Was this translation helpful? Give feedback.
-
|
That is very helpful, thank you @frankie567 . For completeness, this is the final code which meets my requirements: from fastapi import Request, FastAPI, Form, HTTPException
import logging
app = FastAPI()
@app.post("/post_name")
async def parse_data(request: Request):
data_body = await request.form()
logging.info(data_body)
if "first_name" not in data_body or "last_name" not in data_body:
raise HTTPException(422, "Missing fields")
first_name = data_body["first_name"]
last_name = data_body["last_name"]
return first_name + last_name |
Beta Was this translation helpful? Give feedback.
That is very helpful, thank you @frankie567 .
For completeness, this is the final code which meets my requirements: