【发布时间】:2017-04-08 04:47:43
【问题描述】:
如何使用无服务器框架处理多部分/表单数据? v.0.5.6
刚刚试过这个:
"requestTemplates": {
"multipart/form-data": {
"httpMethod": "$context.httpMethod",
"body": "$input.json('$')",
"queryParams": "$input.params().querystring",
"headerParams": "$input.params().header",
"headerParamNames": "$input.params().header.keySet()",
"contentTypeValue": "$input.params().header.get('Content-Type')"
},
"application/json": {
"httpMethod": "$context.httpMethod",
"body": "$input.json('$')",
"queryParams": "$input.params().querystring",
"headerParams": "$input.params().header",
"headerParamNames": "$input.params().header.keySet()",
"contentTypeValue": "$input.params().header.get('Content-Type')"
}
}
action.js:
export function respond(event, cb) {
var form = new formidable.IncomingForm();
form.parse(event, function(err, fields, files) {
if (err == null) {
var response = {
status: "true",
data: fields,
error: []
};
return cb(null, response);
} else {
console.log(err);
return cb(null, ApiErrors.errors(402, err['message'] + fields));
}
});
}
但出现错误:errorMessage = "Cannot read property 'content-length' of undefined";
【问题讨论】:
-
如果
event是一个常规的 Lambda 事件,那么该表单已经为您解析好了。解析来自请求的输入是请求模板的一部分。所以这个责任落在 API Gateway 身上,而不是 Lambda。
标签: multipartform-data serverless-framework