question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] Allow empty Form fields

See original GitHub issue

I see that FastAPI does support Form data input. However it does not seem to support empty form fields and throws error:

{"detail":[{"loc":["body","searchstr"],"msg":"field required","type":"value_error.missing"}]}

Basically, fastapi expects all fields that are going to be handled should not be empty, which is not realistic IMHO. Or can someone show me a workaround?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

37reactions
nsidnevcommented, Jan 14, 2020

@borako FastAPI uses the pydantic’s convention to mark fields as required. As described here, if you use ... in the definition of a query parameter, then FastAPI will require this field in the request input. The same is true for the Form, Body and other parameters from FastAPI. You can set a default value for your field if it can be omitted in the form data. Additionally, you can mark the field as typing.Optional if you don’t want a static analyzer like mypy to complain about types. Here is an example:

from typing import Optional

from fastapi import FastAPI, Form

app = FastAPI()


@app.post("/my-form-endpoint")
def my_endpoint(
    my_field_with_default: str = Form("some value"),
    my_optional_field: Optional[int] = Form(None),
    my_required_field: str = Form(...),
) -> dict:
    return {
        "my_field_with_default": my_field_with_default,
        "my_optional_field": my_optional_field,
        "my_required_field": my_required_field,
    }

There are 3 form fields defined for this endpoint, but FastAPI will require that only my_required_field be present in the user input. Here are some examples of requests to this application using the httpie client:

$ http -f POST :8000/my-form-endpoint my_required_field=something
HTTP/1.1 200 OK
content-length: 95
content-type: application/json
date: Tue, 14 Jan 2020 01:43:21 GMT
server: uvicorn

{
    "my_field_with_default": "some value",
    "my_optional_field": null,
    "my_required_field": "something"
}
$ http -f POST :8000/my-form-endpoint my_required_field=something my_optional_field=42
HTTP/1.1 200 OK
content-length: 93
content-type: application/json
date: Tue, 14 Jan 2020 01:43:37 GMT
server: uvicorn

{
    "my_field_with_default": "some value",
    "my_optional_field": 42,
    "my_required_field": "something"
}
$ http -f POST :8000/my-form-endpoint my_required_field=something my_optional_field=42 my_field_with_default=non-default-value
HTTP/1.1 200 OK
content-length: 100
content-type: application/json
date: Tue, 14 Jan 2020 01:44:06 GMT
server: uvicorn

{
    "my_field_with_default": "non-default-value",
    "my_optional_field": 42,
    "my_required_field": "something"
}
7reactions
borakocommented, Jan 14, 2020

This is great, very helpful. I understand fastapi borrows a lot of pieces from others (starlette, pydantic, etc.). Being not familiar with those technologies, fastapi user guide sections for Form data look too simplistic. It’ll be wonderful if this makes into those sections in the future. Thanks! Bora

Read more comments on GitHub >

github_iconTop Results From Across the Web

allow-empty-form-fields - IBM
If a forms login request is received with either an empty user name or an empty password, then WebSEAL returns the login form...
Read more >
Solved: Allow empty Description (linked field) in Forms
When creating the new Forms request type. I've noticed that if i create my own input field, i can link them to the...
Read more >
How to allow a form to submit, but prevent blank fields from ...
The problem I am running into is, whenever I create an instance of Item using the Admin form and I do not provide...
Read more >
JavaScript : HTML form - Checking for non empty - w3resource
Javascript function to check if a field in a html form is empty or not.
Read more >
How To Add Validation For Empty Input Field with JavaScript
JavaScript validation for empty input field. Try to submit the form without entering any text. Name: Step 1) Add HTML: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found