AWS ApiGateway 请求路径映射 + lambda
AWS ApiGateway request path mapping + lambda
我知道有很多关于映射请求数据的问题,但没有一个对我有帮助。
所以,我想要实现的是一个映射到 lambda 的 API 端点。当桶触发 404 时转发到该端点的请求,并且参数通过请求路径传递给 lambda,例如:/{image_name}/{width}/{height}。
我的 lambda 代码只是调用 context.succeed(event, context);
在方法请求配置中,请求路径的参数是自动创建的,。
在集成请求中,我创建了三个映射模板:plain/text、plain/html、application/json,其定义与以下相同:
#set($inputRoot = $input.path('$'))
{
"name": $input.params('name'),
"width" : $input.params('width'),
"height" : $input.params('height'),
"params": $input.params(),
"resourcePath": $context.resourcePath,
}
当从 chrome 休息客户端调用时,我得到:
从控制台调用测试时,我得到以下响应:
{"Type":"User","message":"Could not parse request body into json."}
当我调用 curl 或在浏览器中打开 URL 时得到的响应相同。
但是在控制台测试调用的日志中我看到:
Execution log for request test-request
Tue Sep 08 09:10:20 UTC 2015 : Starting execution for request: test-invoke-request
Tue Sep 08 09:10:20 UTC 2015 : API Key: test-invoke-api-key
Tue Sep 08 09:10:20 UTC 2015 : Method request path: {name=name, width=100, height=100}
Tue Sep 08 09:10:20 UTC 2015 : Method request query string: {}
Tue Sep 08 09:10:20 UTC 2015 : Method request headers: {}
Tue Sep 08 09:10:20 UTC 2015 : Method request body before transformations: null
Tue Sep 08 09:10:20 UTC 2015 : Endpoint request URI: <endpoint>:function:Magic/invocations
Tue Sep 08 09:10:20 UTC 2015 : Endpoint request headers: {
Authorization=<authorization>
Credential=<credential>,
SignedHeaders=accept;content-type;host;user-agent;x-amz-content-sha256;x-amz-date;x-amz-source-arn,
Signature=<signature>,
X-Amz-Date=20150908T091020Z,
X-Amz-Source-Arn=<ARN>/null/GET/image/{name}/{width}/{height},
Accept=application/json,
User-Agent=AmazonAPIGateway_ebkkwbbpo0,
Host=lambda.us-east-1.amazonaws.com,
X-Amz-Content-Sha256=<key>,
Content-Type=application/json
}
Tue Sep 08 09:10:20 UTC 2015 : Endpoint request body after transformations: {
"name": name,
"width" : 100,
"height" : 100,
"params": {path={name=name, width=100, height=100}, querystring={}, header={}},
"resourcePath": /image/{name}/{width}/{height},
}
Tue Sep 08 09:10:20 UTC 2015 : Endpoint response body before transformations: {"Type":"User","message":"Could not parse request body into json."}
Tue Sep 08 09:10:20 UTC 2015 : Endpoint response headers: {
x-amzn-ErrorType=InvalidRequestContentException:http://internal.amazon.com/coral/com.amazonaws.awsgirapi/,
x-amzn-RequestId=<RequestId>,
Connection=keep-alive,
Content-Length=68,
Date=Tue, 08 Sep 2015 09:10:20 GMT,
Content-Type=application/json}
Tue Sep 08 09:10:20 UTC 2015 : Method response body after transformations: {"Type":"User","message":"Could not parse request body into json."}
Tue Sep 08 09:10:20 UTC 2015 : Method response headers: {Content-Type=application/json}
Tue Sep 08 09:10:20 UTC 2015 : Successfully completed execution
正如我在某些时候看到的那样,URL 路径被正确解析,但我不知道哪里出了问题。
另外,我不知道为什么在 X-Amz-Source-Arn 路径中有一个空值。
谢谢。
问题出在集成请求映射模板上。您应该将字符串类型的字段加双引号,以便稍后可以将它们转换为 JSON。
所以在这个例子中你应该写:
#set($inputRoot = $input.path('$'))
{
"name": "$input.params('name')",
"width" : $input.params('width'),
"height" : $input.params('height'),
"params": "$input.params()",
"resourcePath": "$context.resourcePath",
}
我觉得很奇怪,但这就是解决方案。
此外,您不需要为这种情况编写三个映射模板,您应该只留下 application/json
如果是带路径参数的 lambda 集成,路径参数应在集成请求中映射如下。
转到 Integration Response -> Mapping Templates 并将路径参数的以下映射添加到输入值:
{ "itemId": "$input.params('catalogitemid')"}
我知道有很多关于映射请求数据的问题,但没有一个对我有帮助。
所以,我想要实现的是一个映射到 lambda 的 API 端点。当桶触发 404 时转发到该端点的请求,并且参数通过请求路径传递给 lambda,例如:/{image_name}/{width}/{height}。
我的 lambda 代码只是调用 context.succeed(event, context);
在方法请求配置中,请求路径的参数是自动创建的,
在集成请求中,我创建了三个映射模板:plain/text、plain/html、application/json,其定义与以下相同:
#set($inputRoot = $input.path('$'))
{
"name": $input.params('name'),
"width" : $input.params('width'),
"height" : $input.params('height'),
"params": $input.params(),
"resourcePath": $context.resourcePath,
}
当从 chrome 休息客户端调用时,我得到:
从控制台调用测试时,我得到以下响应:
{"Type":"User","message":"Could not parse request body into json."}
当我调用 curl 或在浏览器中打开 URL 时得到的响应相同。
但是在控制台测试调用的日志中我看到:
Execution log for request test-request
Tue Sep 08 09:10:20 UTC 2015 : Starting execution for request: test-invoke-request
Tue Sep 08 09:10:20 UTC 2015 : API Key: test-invoke-api-key
Tue Sep 08 09:10:20 UTC 2015 : Method request path: {name=name, width=100, height=100}
Tue Sep 08 09:10:20 UTC 2015 : Method request query string: {}
Tue Sep 08 09:10:20 UTC 2015 : Method request headers: {}
Tue Sep 08 09:10:20 UTC 2015 : Method request body before transformations: null
Tue Sep 08 09:10:20 UTC 2015 : Endpoint request URI: <endpoint>:function:Magic/invocations
Tue Sep 08 09:10:20 UTC 2015 : Endpoint request headers: {
Authorization=<authorization>
Credential=<credential>,
SignedHeaders=accept;content-type;host;user-agent;x-amz-content-sha256;x-amz-date;x-amz-source-arn,
Signature=<signature>,
X-Amz-Date=20150908T091020Z,
X-Amz-Source-Arn=<ARN>/null/GET/image/{name}/{width}/{height},
Accept=application/json,
User-Agent=AmazonAPIGateway_ebkkwbbpo0,
Host=lambda.us-east-1.amazonaws.com,
X-Amz-Content-Sha256=<key>,
Content-Type=application/json
}
Tue Sep 08 09:10:20 UTC 2015 : Endpoint request body after transformations: {
"name": name,
"width" : 100,
"height" : 100,
"params": {path={name=name, width=100, height=100}, querystring={}, header={}},
"resourcePath": /image/{name}/{width}/{height},
}
Tue Sep 08 09:10:20 UTC 2015 : Endpoint response body before transformations: {"Type":"User","message":"Could not parse request body into json."}
Tue Sep 08 09:10:20 UTC 2015 : Endpoint response headers: {
x-amzn-ErrorType=InvalidRequestContentException:http://internal.amazon.com/coral/com.amazonaws.awsgirapi/,
x-amzn-RequestId=<RequestId>,
Connection=keep-alive,
Content-Length=68,
Date=Tue, 08 Sep 2015 09:10:20 GMT,
Content-Type=application/json}
Tue Sep 08 09:10:20 UTC 2015 : Method response body after transformations: {"Type":"User","message":"Could not parse request body into json."}
Tue Sep 08 09:10:20 UTC 2015 : Method response headers: {Content-Type=application/json}
Tue Sep 08 09:10:20 UTC 2015 : Successfully completed execution
正如我在某些时候看到的那样,URL 路径被正确解析,但我不知道哪里出了问题。 另外,我不知道为什么在 X-Amz-Source-Arn 路径中有一个空值。
谢谢。
问题出在集成请求映射模板上。您应该将字符串类型的字段加双引号,以便稍后可以将它们转换为 JSON。 所以在这个例子中你应该写:
#set($inputRoot = $input.path('$'))
{
"name": "$input.params('name')",
"width" : $input.params('width'),
"height" : $input.params('height'),
"params": "$input.params()",
"resourcePath": "$context.resourcePath",
}
我觉得很奇怪,但这就是解决方案。
此外,您不需要为这种情况编写三个映射模板,您应该只留下 application/json
如果是带路径参数的 lambda 集成,路径参数应在集成请求中映射如下。
转到 Integration Response -> Mapping Templates 并将路径参数的以下映射添加到输入值:
{ "itemId": "$input.params('catalogitemid')"}