API网关映射模板:将表单数据(POST)请求转换为JSON

API网关映射模板:将表单数据(POST)请求转换为JSON,json,amazon-web-services,aws-lambda,postman,aws-api-gateway,Json,Amazon Web Services,Aws Lambda,Postman,Aws Api Gateway,我正在从邮递员那里发送一些表格数据。在aws API网关中,我将内容类型设置为“多部分/表单数据”,映射模板如下: #set($allParams = $input.params()) { "body-json" : $input.json('$'), "params" : { #foreach($type in $allParams.keySet()) #set($params = $allParams.get($type)) "$type" : { #foreach(

我正在从邮递员那里发送一些表格数据。在aws API网关中,我将内容类型设置为“多部分/表单数据”,映射模板如下:

    #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"
    }
}
但是,当我在lambda函数上获得请求时,除了请求主体之外,所有内容都正确地映射到JSON中。请求主体以字符串形式出现。以下是完整的请求:

  {
    "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": {}
}
我对此有两个疑问:

  • 在API网关映射模板本身中,是否有任何方法可以将请求体转换为JSON对象?(表示请求体应该作为JSON对象而不是字符串)
  • 或者我必须在java代码中的lambda处理程序中将请求体字符串转换为JSON对象

  • 非常感谢您的帮助,因为我是AWS新手。

    您可以在body mapping模板中使用$util.parseJson()。它接受“stringified”JSON并返回结果的对象表示形式。您可以使用此函数的结果来访问和操作有效负载的元素


    请阅读文档

    最好以
    application/json
    的形式发送有效负载。这样,您就可以在映射模板中使用
    $util.parseJson()
    解析它,请求主体将作为一个对象出现

    如果您想坚持使用表单数据,则必须在处理程序中对其进行转换


    在Node.js中,我使用
    querystring
    模块来实现此目的。不确定在Java中可以使用什么。

    这是我试图避免使用API网关映射模板的原因之一。使用Lamda代理集成要容易得多。也许这会有所帮助:谢谢,但我之前读过这篇文章,我没有从中得到任何解决方案。但是他没有字符串化JSON?请看上面的问题。我不能将有效负载作为application/json发送,因为客户端的需求是表单数据。不过,现在我正在寻找另一种解决方案,在处理程序中,我将解析该请求正文字符串。@PallaviKaushik您看起来像这样吗?“正文”:“$util.escapeJavaScript($input.json('$))”