The event body is not getting encoded while invoking sam local start-api
and sending a multipart request but it's being encoded in the cloud. So, I'd like to have the same behavior in my local environment.
Steps to reproduce the issue:
- Create the Hello World project provided by
sam init
- Add a post method
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: post
ContentHandling: CONVERT_TO_BINARY # This is not doing the magic as I was expecting
BinaryMediaTypes:
- "*/*"
- multipart/form-data
- Return the isBase64Encoded in the handler.
exports.lambdaHandler = async (event, context) => {
try {
// const ret = await axios(url);
response = {
'statusCode': 200,
'body': JSON.stringify({
message: event.isBase64Encoded,
// location: ret.data.trim()
})
}
} catch (err) {
console.log(err);
return err;
}
return response
};
- Perform the HTTP request:
curl --location --request POST 'http://127.0.0.1:3000/hello' \
--header 'saa: csv/csv-file' \
--form 'foo=@/home/user/csv-file.csv'
- The response is always the same:
{
"message": false
}
I've tried to use the proxy integration but it didn't work.
My workaround is to have something like this in the handler:
const csvString = event.isBase64Encoded ? Buffer.from(event.body, 'base64').toString('utf-8') : event.body;