1

我知道有很多关于映射请求数据的问题,但都没有人帮助我。 所以,我试图实现的是一个映射到lambda的API端点。当一个桶触发一个404并且参数通过请求路径传递给lambda时,对该端点的请求被转发,如:/ {image_name}/{width}/{height}。 我拉姆达的代码只需调用context.succeed(event, context);AWS ApiGateway请求路径映射+ lambda

在请求路径的参数自动创建,proof方法要求配置。

在我已经创建了三个映射模板的合并请求:纯/文本,一般/ HTML,应用/ JSON具有相同的定义波纹管:

#set($inputRoot = $input.path('$')) 
{ 
    "name": $input.params('name'), 
    "width" : $input.params('width'), 
    "height" : $input.params('height'), 
    "params": $input.params(), 
    "resourcePath": $context.resourcePath, 
} 

当调用形成铬REST客户端,我得到:

当调用从控制台的测试,我得到以下回应: {"Type":"User","message":"Could not parse request body into json."} 相同的反应,当我打电话卷曲或当我只需在浏览器中打开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中存在路径中的空值。

谢谢。

回答

9

问题是集成请求映射模板。您应该双引号字符串类型的字段,以便稍后可以将其转换为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

+0

这应该被接受的答案 – ken