Search code examples
pythonfastapix-www-form-urlencoded

Set required form fields using FastAPI


I am building an application in FastAPI and need to receive form fields (x-www-form-urlencoded). I used this page to figure out what to do, And I found I had to use the class: Form(...). The problem is that I want to set the fields as required (to appear in the documentation as required, and also that if they are missing the app will return 422). Form has one required argument, which is: default, but once the default is set the field is no required. I tried to write: Form(None), but it did not help. Is there a way to solve the problem? Many thanks in advance.


Solution

  • Any parameter defined as Form(...) (including the three dots) in your view signature will be required by default. Make sure that you actually include the three dots - they do have a purpose.