4

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:

  1. Create the Hello World project provided by sam init
  2. 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
  1. 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
};
  1. 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'
  1. 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;
petey
  • 16,914
  • 6
  • 65
  • 97
Juanchi Rios
  • 119
  • 6
  • Your `lambdaHandler` is hardcoded to return `{"message": false}`. If you want that to be different modify the code. – petey Aug 13 '20 at 19:52
  • That's not true, the function is returning `message: event.isBase64Encoded` . – Juanchi Rios Aug 21 '20 at 21:28
  • No. try running `JSON.stringify({message: event.isBase64Encoded})` in a console.log if you don t believe it – petey Aug 25 '20 at 15:18

0 Answers0