Reputation: 499
I'm trying to do a multipart/form-data
POST containing a pdf file to an AWS lambda function, do a few checks on the file and then POST the file to an external API.
I'm using the serverless framework with node.js and the aws-lambda-multipart-parser plugin to receive the file.
The file upload from the client to lambda works, and I can print the buffer content to the console. The part that does not work is the forwarding of the file to an external API.
This is my current axios setup to make a POST request to the external API:
const formData = new FormData();
formData.append("file", file.content, file.filename);
const res = await axios.post(`https://external-api.com`, formData, {
headers: { ...formData.getHeaders(), "Access-Control-Allow-Origin": "*" },
params: {
someParams: foo
}
});
file.content
actually contains a Buffer with the file content, but when I make the request, the external API returns an error and the request fails...
Interestingly, the whole setup works when I run the serverless function locally with sls offline
. That may suggest that there is a problem with the API Gateway setup.
Some additional info:
Based on the instructions from the multipart plugin I added the binary data type in the API Gateway settings:
This is the config of the endpoint in AWS Gateway:
I also tried this SO question and the AWS documentation, but they only cover how to receive a binary file, not how to send one.
Is it possible to receive and then send a file in lambda, or should this be solved in a different way?
Upvotes: 7
Views: 10748
Reputation: 21
I had the same problem with that library. So I tried with - https://www.npmjs.com/package/aws-multipart-parser - there is also a description of what to write in serverless.yml
file. It is working like charm now!
Upvotes: 1