Parser of multipart/form-data requests for AWS Lambda
npm install aws-lambda-multipart-parser
method: POST
integration: LAMBDA
`
method: POST - because of we are receiving multipart/form-data request.
integration: LAMBDA - without that I constanly got 502 Error: Bad Gateway, but I don't know why.
$3
Execute command sls deploy in AWS Lambda folder. (for more information look for sources at step 1)
$3
1. Go to API Gateway.
2. Select your API in API Gateway interface
!capture
3. Go to Settings
!capture
4. Add multipart/form-data binary media type
!capture
5. Go to Resources -> POST method of your API -> Integration Request
!capture
6. Check Use Lambda Proxy Integration
!capture
7. Deploy your API changes
!capture
$3
In majority of cases, while working with AWS Lambda, you will need to enable CORS. Unfortunately, standard way of doing it with AWS UI in API Gateway doesn't work in our case. To solve the problem, you need to send Access-Control-Allow-Origin header as a part of response from your lambda. For example:
`
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(parse(event, true)),
};
callback(null, response);
`
$3
1. Import aws-lambda-multipart-parser with npm install --save aws-lambda-multipart-parser command.
2. Require it in file const multipart = require('aws-lambda-multipart-parser');.
3. Pass your event object to parse function like that multipart.parse(event, spotText), where event is a event object you get from lambda invocation, spotText - if it's true all text files are present in text for after parsing, if it's false all text files are represented as regular files with Buffer. Parse function will return object representing the request body.
$3
`
{
"file": {
"type": "file",
"filename": "lorem.txt",
"contentType": "text/plain",
"content": {
"type": "Buffer",
"data": [ ... byte array ... ]
} or String
},
"field": "value"
}
``