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