-
|
Hi team. I have an Update endpoint on some data in my backend. But if I send an empty string, fastapi replace it by None. I tried notes: _t.Optional[str] = Form(None),and notes: _t.Optional[str] = None,But every time it is transformed to None. Is there a way to accept and keep empty strings ? I am using FastAPI version 0.54.2. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 13 comments
-
|
Above details are not sufficient to define the issue(follow issue template). |
Beta Was this translation helpful? Give feedback.
-
|
Sorry I should have given more details. On latest version of fastapi (0.63.0): from typing import Optional
from fastapi import FastAPI, Form
app = FastAPI()
@app.post("/items")
async def create_item(
item_description: Optional[str] = Form(None),
):
print(item_description)
print(item_description is None)
print(item_description == '')If I post on the endpoint: as a result I get: I expect item_description to be an empty string, not None. I want to be able to differentiate those 2 calls. No data: Data empty: |
Beta Was this translation helpful? Give feedback.
-
|
Have you tried this one?
Here..
|
Beta Was this translation helpful? Give feedback.
-
|
It sounds like you just want the default value to be an empty string? If that's the case, just change the value that from fastapi import FastAPI, Form, testclient
app = FastAPI()
@app.post("/items")
async def create_item(
item_description: str = Form(""),
):
print(item_description)
print(item_description is None)
print(item_description == "") |
Beta Was this translation helpful? Give feedback.
-
|
yes, it depends on the default value. |
Beta Was this translation helpful? Give feedback.
-
|
I can't use
Ok, it seems to be working ! I was missing the empty string in my data, so it was defaulting to None. There is indead a difference between: and Now I just need to find how to send an empty string with axios. it seems to replace it with empty data, so the backend default to None.. Thank you so much for your help. |
Beta Was this translation helpful? Give feedback.
-
I'm reopening because I find it is a very strange behavior. Is there no way to differentiate no param
if item_description == "''":
do_something() |
Beta Was this translation helpful? Give feedback.
-
It is awkward indeed 🤣 Wouldn't it be better if you handle all the validation logic inside the Pydantic model? |
Beta Was this translation helpful? Give feedback.
-
|
Yes, awkward solution for an awkward curl in the description..😉 As @ycd suggested, you can handle it in pydantic class as per your usecase. |
Beta Was this translation helpful? Give feedback.
-
|
Ok, I don't have this problem when I am using pydantic objects something like this: from pydantic import BaseModel, validator
class Item(BaseModel):
name: Optional[str] = None
description: Optional[str] = None
@app.post("/items")
async def create_item(
item: Item,
):
returnThe data send in the request is a json, and I can ommit some field or send empty value.
Thank you all for your help! |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here everyone! 👏 🙇 Thanks for reporting back and closing the issue @VictorBac 👍
|
Beta Was this translation helpful? Give feedback.
-
|
I agree with the thread starter @VictorBac That in FastAPI (
In both cases I get the value The main problem is that these 2 states are mixed together and I cannot distinguish them. For example, my old backend application written on |
Beta Was this translation helpful? Give feedback.
-
|
htmx sends unquoted form fields and so when a textbox is empty, we get missing field instead of empty string. greenfield is not so bad, but it seems awkward to have to give a default value for a required field if empty string is a valid value. I'd like to reserve 422 for when we actually have a bug and the field is actually missing. |
Beta Was this translation helpful? Give feedback.
It sounds like you just want the default value to be an empty string?
If that's the case, just change the value that
item_descriptionis assigned to be Form(""). The first argument of the param function is the default value assigned.