如何解决Sam在本地调用时未编码请求主体
在调用sam local start-api
并发送多部分请求时,事件主体未得到编码,但已在云中进行了编码。因此,我希望在本地环境中具有相同的行为。
重现问题的步骤:
- 创建
sam init
提供的 Hello World 项目
- 添加发布方法
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
- 返回处理程序中的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
};
- 执行HTTP请求:
curl --location --request POST 'http://127.0.0.1:3000/hello' \
--header 'saa: csv/csv-file' \
--form 'foo=@/home/user/csv-file.csv'
- 响应始终是相同的:
{
"message": false
}
我尝试使用代理集成,但是没有用。
我的解决方法是在处理程序中添加以下内容:
const csvString = event.isBase64Encoded ? Buffer.from(event.body,'base64').toString('utf-8') : event.body;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。