Rihab Sabri
Rihab Sabri

Reputation: 1

How to fix Error 500 when sending a file in a Formdata?

I need to send a file to my server I m using a FormData and I specified the headers, But I keep getting the same error: 500 which is not telling me a lot, When I try to inspect the network in dev tools I see this message from the server:

Traceback (most recent call last): File "/var/task/aws_lambda_powertools/event_handler/api_gateway.py", line 611, in _call_route return ResponseBuilder(self._to_response(route.func(**args)), route) File "/var/task/routes/leads.py", line 47, in upload_files files_url, images_url = uploadFilesLeads(body, headers) File "/var/task/services/leads.py", line 30, in uploadFilesLeads files, images = get_file_from_request_body(headers, body) File "/var/task/repository/shared.py", line 25, in get_file_from_request_body "content-type": headers["Content-Type"], KeyError: 'Content-Type'

This is my post request :

const attchFiles = new FormData();
attchFiles.append("files", files);
const attachements = await axios.post(MyURL, attchFiles, {
  headers: {
    "Content-Type": "multipart/form-data",
  },
});
console.log(attachements);

Upvotes: 0

Views: 2211

Answers (1)

Quentin Grau
Quentin Grau

Reputation: 61

You need to add the appropriate headers:

headers: formData.getHeaders()

So in your case:

const attchFiles = new FormData();
attchFiles.append("files", files);
const attachements = await axios.post(MyURL, attchFiles, {
  headers: attachFiles.getHeaders()
});
console.log(attachements);

Indeed, you were missing the "boundary" header.

Upvotes: 1

Related Questions