Sam在本地调用时未编码请求主体

问题描述

调用sam local start-api并发送多部分请求时,事件主体未得到编码,但已在云中进行了编码。因此,我希望在本地环境中具有相同的行为。

重现问题的步骤:

  1. 创建sam init
  2. 提供的 Hello World 项目
  3. 添加发布方法
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. 返回处理程序中的isBase64Encoded。
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. 执行HTTP请求:
curl --location --request POST 'http://127.0.0.1:3000/hello' \
--header 'saa: csv/csv-file' \
--form 'foo=@/home/user/csv-file.csv'
  1. 响应始终是相同的:
{
    "message": false
}

我尝试使用代理集成,但是没有用。

我的解决方法是在处理程序中添加以下内容

const csvString = event.isBase64Encoded ? Buffer.from(event.body,'base64').toString('utf-8') : event.body;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)