[英]API Gateway mapping template : Convert form-data(POST) request to JSON
I am sending some form-data in request body from postman. 我正在邮递员的请求正文中发送一些表单数据。 In aws API Gateway I have set Content-type as "multipart/form-data" and mapping template is following:
在AWS API Gateway中,我已将Content-type设置为“ multipart / form-data”,并且映射模板如下:
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
#set($params = $allParams.get($type))
"$type" : {
#foreach($paramName in $params.keySet())
"$paramName" : "$util.escapeJavaScript($params.get($paramName))"
#if($foreach.hasNext),#end
#end
}
#if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
#if($foreach.hasNext),#end
#end
},
"context" : {
"account-id" : "$context.identity.accountId",
"api-id" : "$context.apiId",
"api-key" : "$context.identity.apiKey",
"authorizer-principal-id" : "$context.authorizer.principalId",
"caller" : "$context.identity.caller",
"cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
"cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
"cognito-identity-id" : "$context.identity.cognitoIdentityId",
"cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
"http-method" : "$context.httpMethod",
"stage" : "$context.stage",
"source-ip" : "$context.identity.sourceIp",
"user" : "$context.identity.user",
"user-agent" : "$context.identity.userAgent",
"user-arn" : "$context.identity.userArn",
"request-id" : "$context.requestId",
"resource-id" : "$context.resourceId",
"resource-path" : "$context.resourcePath"
}
}
However when I am getting the request on my lambda function everything is properly mapped into JSON, except my request body. 但是,当我在lambda函数上获取请求时,除了我的请求正文之外,所有内容都已正确映射到JSON中。 Request body is coming as String.
请求主体以String形式出现。 Following is the complete request:
以下是完整的请求:
{
"context": {
"authorizer-principal-id": "",
"cognito-authentication-type": "",
"cognito-identity-id": "",
"resource-path": "/env/Response",
"account-id": "",
"cognito-identity-pool-id": "",
"request-id": "cd2052-11e7-92b6-e3c7d7dgh01",
"api-id": "39dhjsr8",
"resource-id": "skdsk5",
"user-arn": "",
"caller": "",
"http-method": "POST",
"cognito-authentication-provider": "",
"api-key": "",
"user": "",
},
"body-json": "------WebKitFormBoundaryLAn6jCAA10bF7ZMx\r\nContent-Disposition: form-data; name=\"sig\"\r\n\r\nmcHeelgDQcYnjh5L2L92H8KLLE=\r\n------WebKitFormBoundaryLAn6jCAA10bF7ZMx\r\nContent-Disposition: form-data; name=\"resultdescription\"\r\n\r\ncancelled.\r\n------WebKitFormBoundaryLAn6jCAA10bF7ZMx\r\nContent-Disposition: form-data; name=\"result\"\r\n\r\nF\r\n------WebKitFormBoundaryLAn6jCAA10bF7ZMx\r\nContent-Disposition: form-data; name=\"errorcode\"\r\n\r\n101\r\n------WebKitFormBoundaryLAn6jCAA10bF7ZMx\r\nContent-Disposition: form-data; name=\"id\"\r\n\r\n420LU2UEG\r\n------WebKitFormBoundaryLAn6jCAA10bF7ZMx\r\nContent-Disposition: form-data; name=\"key\"\r\n\r\ntdx\r\n------WebKitFormBoundaryLAn6jCAA10bF7ZMx\r\nContent-Disposition: form-data; name=\"refid\"\r\n\r\n10480\r\n------WebKitFormBoundaryLAn6jCAA10bF7ZMx\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\nTest\r\n------WebKitFormBoundaryLAn6jCAA10bF7ZMx\r\nContent-Disposition: form-data; name=\"date\"\r\n\r\n2016-02-28T20:05:05.6330000Z\r\n------WebKitFormBoundaryLAn6jCAA10bF7ZMx\r\nContent-Disposition: form-data; name=\"timestamp\"\r\n\r\n2016-02-29T04:15:47.4797Z\r\n------WebKitFormBoundaryLAn6jCAA10bF7ZMx--",
"params": {
"path": {},
"querystring": {},
"header": {
},
"stage-variables": {}
}
I have 2 queries regarding this: 我对此有2个查询:
Any help will be highly appreciated, as I am new to AWS. 非常感谢您的帮助,因为我是AWS新手。
It would be best if you send your payload as application/json
. 最好将有效负载作为
application/json
发送。 That way, you can have it parsed in the mapping template using $util.parseJson()
and the request body will come out as an object. 这样,您可以使用
$util.parseJson()
在映射模板中对其进行解析,并且请求主体将作为对象出现。
If you want to stick with form-data, you'll have to convert it inside your handler. 如果要坚持使用表单数据,则必须在处理程序中进行转换。
In Node.js, I use querystring
module for this. 在Node.js中,我为此使用
querystring
模块。 Not sure what you can use in Java. 不知道可以在Java中使用什么。
You can use $util.parseJson() in body mapping template. 您可以在主体映射模板中使用$ util.parseJson() 。 It takes "stringified" JSON and returns an object representation of the result.
它采用“字符串化” JSON,并返回结果的对象表示形式。 You can use the result from this function to access and manipulate elements of the payload
您可以使用此函数的结果来访问和操纵有效载荷的元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.