简体   繁体   English

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

[英]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个查询:

  1. Is there any way where I can convert request body into JSON Object in API gateway mapping template itself?(Means instead of String, Request Body should come as JSON Object). 有什么方法可以在API网关映射模板本身中将请求主体转换为JSON对象?(意味着不是String,而是请求主体应为JSON对象)。
  2. Or I have to convert request body String into JSON Object at lambda handler in my java code? 还是我必须在Java代码中的lambda处理程序中将请求正文字符串转换为JSON对象?

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 您可以使用此函数的结果来访问和操纵有效载荷的元素

Please read the documentation here 请在此处阅读文档

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

相关问题 将 rest api POST 请求从 json 转换为 golang 中的表单数据 - convert rest api POST request from json to form-data in golang 将json post请求更改为multipart / form-data - Change a json post request to multipart/form-data 使用 axios 在 POST 多部分/表单数据请求中发送文件和 json - sending file and json in POST multipart/form-data request with axios AWS API 网关映射模板 JSON - AWS API Gateway Mapping Template JSON 改造表单数据POST不返回JSON - Retrofit form-data POST not return JSON 是否可以在 POST 请求正文中发送 Json 数据和图像作为表单数据 - Is it possible to send Json Data in the POST request body and an image as form-data 邮递员:通过表单数据的嵌套 JSON 的 POST 请求不起作用(而通过原始数据可以) - Postman: POST request of nested JSON via form-data not working (while via raw-data ok) 邮递员在尝试使用表单数据执行 POST 时返回 415,但与 JSON 相同的请求工作正常 - Postman returns 415 when trying to do a POST using form-data, but the same request with JSON works fine 使用主体表单数据的nativescript发布请求 - nativescript post request using body form-data 帖子中的文件请求有效内容为空,表单数据的文件名为空 - Empty file request payload on post and empty filename for form-data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM