I have this example backend in Fastapi:
@router.post("/api/v1/upload")
def upload_file(file: UploadFile = File(...)):
print(file)
return 200
In the frontend I am not able to post the image successfully getting all the time 422 Unprocessable Entity.
<form @submit.prevent="handleUpload" class="space-y-6" >
<div>
<input type="file" @change="handleFileInputChange" accept="image/jpeg, image/png,
image/jpg">
</div>
<button type="submit">UPLOAD</button>
Script
async function handleFileInputChange (event) {
const formData = new FormData();
formData.append("file", event.target.files[0])
and the POST part
async function handleUpload() {
const url = "http://0.0.0.0:8000/api/v1/upload"
const requestOptions = {
method: "POST",
headers: {
'Content-Type': 'multipart/form-data'
},
body: { file: formdata.file },
}
const data = await $fetch(url, requestOptions)
This is not working I have tried all options of Headers like 'Accept': '/','Content-Type': 'undefined',
I have tried this as well:
body: JSON.stringify({file: formdata.file})
Any help here? Probably is something fundamental that I am missing.