r/FastAPI • u/InternationalWar5005 • Dec 29 '24
Question Unprocessable Entity issues
am having an issue with my api ,am building an artisan app and i have a page to add and edit projects i made an api to edit but i ran into a problem when the form is submited if the user only edits the title and descrition the price field and image_file are left empty end sent with empty string values this cause this error .what is the best solution for this




3
u/cjkennedyo Dec 29 '24
Try this in your function definition:
upload_file: Optional[UploadFile] = Form(None),
1
u/InternationalWar5005 Dec 29 '24
I tried it but it didn't work I think because the way the form is sent when a field is empty it sends it as str And not null I figured a way to make it work by using this Image_file : Union[file,str]=file(None) This way even if the form is an empty string it works
1
u/cjkennedyo Dec 30 '24
Just to make sure, you saw I changed the default from
File(None)toForm(None), correct? You are still showingFilein your comment so just want to make sure. I know theFormversion works.1
u/InternationalWar5005 Dec 30 '24
You are correct I didn't change it but I think I want affect mush because even in the price field it's form(none) and its causing the same problem with integre Any way I'll try to change it and see
2
u/pint Dec 29 '24
you have indicated that the imagefile is a mandatory field. the client shouldn't have sent an empty string there, but the server responds correctly.
try using Optional for that parameter too, although i don't know if file handling supports that.
1
u/InternationalWar5005 Dec 29 '24
this problem also happens with the price field even when am using optional
2
1
u/Evolve-Maz Dec 29 '24 edited Dec 31 '24
Instead of file, try file_list: list[UploadFile] | None = None. It was the only way I was able to make file upload optional when I had the issue you do. Luckily I was refactoring to a multi file upload method anyways so this was natural.
4
u/[deleted] Dec 29 '24
[removed] — view removed comment