Skip to content

Automatic OPTIONS request with route schema #2008

@samuelcolvin

Description

@samuelcolvin

First check

Yes to all (great list of checks btw, I might copy them to pydantic 👍 )

Thank you

@tiangolo FastAPI is great. This is the first time I've used it in production and I'm really impressed by both the range of features and particularly the documentation. I've written and deployed web apps using django, flask, aiohttp, starlette and now fastapi. FastAPI is the only python web framework I've seen to come close to django in terms of quality of documentation, it's definitely now my go-to web framework.

Thank you for the amazing project, I feel really proud to have contributed to it indirectly.

Feature Request

The only thing I've seen missing from fastpi is support for informative OPTIONS requests for all/most/some endpoints, automatically included based in the route's definition.

I effectively want the following:

class LoginForm(BaseModel):
    email: EmailStr
    password: SecretStr

@app.post('/login/')
async def login(login_form: LoginForm):
   ...

@app.options('/login/', include_in_schema=False)
async def login_options():
    return LoginForm.schema(by_alias=True)

Without having to define the second endpoint. This is so my react frontend can build forms without having to duplicate the field definitions.

I guess it might have to be slightly more complex to include parameters, query args and body model(s).

I know I can get the information I need from /openapi.json, but that involves more processing in js and more data downloaded.

In the past when I've implemented a feature like this myself, I had to deal with OPTIONS requests associated with preflight checks separately, but that's relatively easy by looking for the Access-Control-Request-Method header.

I guess if this feature was to be approved it would have to be optional, default off. I guess through a keyword arg on FastAPI, include_router and each route.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions