Thank you very much for developing and maintaining FastAPI, it's great 馃檪
I tried to create a POST route with username: str and password: str parameters in the function signature. I was expecting FastAPI to search for username and password in the request body (since I used @app.post), but instead it gets them from the URL, as query parameters. It seems it only works when the parameter is a Pydantic model, not a builtin type.
With
@app.post("/route/")
def my_route(username: str, password: str):
...
Accept username and password as fields of the request body, not as query parameters.
Use Pydantic models. It works fine, it's just a bit less straight-forward.
class LoginForm(BaseModel):
username: str
password: str
@app.post("/route/")
def my_route(login_data: LoginForm):
...
It could also be emphasized in the docs (https://fastapi.tiangolo.com/tutorial/body/) that it will not work with built-in types, only with Pydantic models.
I'm not sure my feature request makes sense: generally speaking, if POST requests can also use query parameters in the URL, then it's impossible to distinguish the ones that should be in the request body and the ones that appear in the URL as query parameters. It means it would not be possible to build the openapi.json I guess. In that case, please close this as invalid 馃檪
@pawamoy
Hi! I think you are looking for this part of the documentation. Is that what you need?
So your example will look something like this:
from fastapi import FastAPI, Body
app = FastAPI()
@app.post("/route/")
def my_route(username: str = Body(...), password: str = Body(...)) -> None:
print("username:", username)
print("password length:", len(password))
Indeed! Thank you for pointing me to this page. This feature request can definitely be closed. I'm really sorry for the noise! Cheers
Thanks @nsidnev for your help here! :nerd_face: :cake:
Thanks @pawamoy for reporting back and closing the issue :taco: :rocket:
Most helpful comment
@pawamoy
Hi! I think you are looking for this part of the documentation. Is that what you need?
So your example will look something like this: