I am getting a HTTP 422 when attempting to upload a file from an Angular v13 application to FastAPI. I got great search results in a few different places 1, 2, 3, 4. However none of the solutions worked.
The FastAPI backend code/method as the following signature.
@router.post('/v1/file')
async def create_file(request: Request, file: UploadFile = File(...)):
...
The Angular code looks like the following.
fileSelected($event: any) {
const file: File = $event.target.files[0];
const data = new FormData();
data.append('file', file, file.name);
this.http.post<any>('http://localhost:8000/v1/file', data).subscribe({
next: r => console.log(r),
error: e => console.error(e)
})
}
But I am still getting the following error: POST http://localhost:8000/v1/file 422 (Unprocessable Entity)
.
The HttpErrorResponse shows the following information
- error.detail[0].loc = ['body', 'file']
- error.detail[0].msg = 'field required'
- error.detail[0].type = 'value_error.missing'
Any ideas on what I am doing wrong?