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



我正在从邮递员的请求正文中发送一些表单数据。在 aws API Gateway 中,我将内容类型设置为"多部分/表单数据",映射模板如下:

    #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": "------WebKitFormBoundaryLAn6jCAA10bF7ZMxrnContent-Disposition: form-data; name="sig"rnrnmcHeelgDQcYnjh5L2L92H8KLLE=rn------WebKitFormBoundaryLAn6jCAA10bF7ZMxrnContent-Disposition: form-data; name="resultdescription"rnrncancelled.rn------WebKitFormBoundaryLAn6jCAA10bF7ZMxrnContent-Disposition: form-data; name="result"rnrnFrn------WebKitFormBoundaryLAn6jCAA10bF7ZMxrnContent-Disposition: form-data; name="errorcode"rnrn101rn------WebKitFormBoundaryLAn6jCAA10bF7ZMxrnContent-Disposition: form-data; name="id"rnrn420LU2UEGrn------WebKitFormBoundaryLAn6jCAA10bF7ZMxrnContent-Disposition: form-data; name="key"rnrntdxrn------WebKitFormBoundaryLAn6jCAA10bF7ZMxrnContent-Disposition: form-data; name="refid"rnrn10480rn------WebKitFormBoundaryLAn6jCAA10bF7ZMxrnContent-Disposition: form-data; name="name"rnrnTestrn------WebKitFormBoundaryLAn6jCAA10bF7ZMxrnContent-Disposition: form-data; name="date"rnrn2016-02-28T20:05:05.6330000Zrn------WebKitFormBoundaryLAn6jCAA10bF7ZMxrnContent-Disposition: form-data; name="timestamp"rnrn2016-02-29T04:15:47.4797Zrn------WebKitFormBoundaryLAn6jCAA10bF7ZMx--",
    "params": {
        "path": {},
        "querystring": {},
        "header": {
           },
    "stage-variables": {}
}

我对此有 2 个疑问:

  1. 有什么方法可以将请求正文转换为 API 网关映射模板本身中的 JSON 对象?(意味着请求正文应作为 JSON 对象而不是字符串)。
  2. 或者我必须在我的 java 代码中的 lambda 处理程序中将请求正文字符串转换为 JSON 对象?

任何帮助将不胜感激,因为我是 AWS 的新手。

最好将

有效负载作为application/json发送。这样,您可以使用$util.parseJson()在映射模板中对其进行解析,请求正文将作为对象出现。

如果你想坚持使用表单数据,你必须在你的处理程序中转换它。

在 Node.js 中,我为此使用了querystring模块。不确定你可以在 Java 中使用什么。

您可以在正文映射模板中使用 $util.parseJson()。它采用"字符串化"JSON,并返回结果的对象表示形式。您可以使用此函数的结果来访问和操作有效负载的元素

请在此处阅读文档

最新更新