1

I am sending file in formData from typescript using Fetch API and hitting FastAPI to upload the file to amazon s3. When I am using the api docs it is working and flie is uploading but then I am hitting it with fetch error is coming -> There was an error parsing the body.

On the typescript side...

var formData = new FormData();
formData.append('file', files[0]);

const requestOptions = {
            method: requestMethod,
            headers: 
            {
                'Content-Type': 'multipart/form-data',
                'Authorization': `Bearer ${token}`,
            },
            body : formData,
            async: false,
            contentType: false,
            processData: false,
            };
let response = await fetch(url, requestOptions);

On FastAPI side...

@router.post("/Upload_video_to_s3_for_module")
async def upload_video_to_s3_for_module(
    *,
    db: Session = Depends(deps.get_db),
    file: UploadFile = File(...),
    ) -> Any:
    // code to upload the video file.
Chris
  • 18,724
  • 6
  • 46
  • 80
  • What is the body of the response when you get the error? – MatsLindh Apr 20 '23 at 10:33
  • in response I am getting {"detail":"There was an error parsing the body"}. – nihar saini Apr 20 '23 at 10:35
  • Have you verified that `files[0]` is what you expect? i.e. you usually would access `files[0]` on a file upload input element, not directly on `files`, but you might have reassigned it or have it as a parameter to your function? – MatsLindh Apr 20 '23 at 10:53
  • Please have a look at these related answers as well: [here](https://stackoverflow.com/a/71711008/17865804), [here](https://stackoverflow.com/a/71146753/17865804) and [here](https://stackoverflow.com/a/74810115/17865804) – Chris Apr 20 '23 at 11:04
  • For further related answers regarding uploading a file to amazon s3 using FastAPI, see [here](https://stackoverflow.com/a/70653605/17865804), as well as [here](https://stackoverflow.com/a/70665801/17865804) and [here](https://stackoverflow.com/a/73815575/17865804) – Chris Apr 20 '23 at 11:08

0 Answers0