繁体   English   中英

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

[英]API Gateway mapping template : Convert form-data(POST) request to JSON

我正在邮递员的请求正文中发送一些表单数据。 在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"
    }
}

但是,当我在lambda函数上获取请求时,除了我的请求正文之外,所有内容都已正确映射到JSON中。 请求主体以String形式出现。 以下是完整的请求:

  {
    "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": {}
}

我对此有2个查询:

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

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

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

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

在Node.js中,我为此使用querystring模块。 不知道可以在Java中使用什么。

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

请在此处阅读文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM