I successfully created and tested in API Gateway Console a REST Service Proxy API returning all items in a DynamoDB table using Scan API. And to make DB's response as a JSON object, I added a mapping template in 'Integration Response' which transforms the response as follows:
{
"tableItems": [
{ // item_1
"attribute_1": "value",
"attribute_2": "someValue"
},
{ //item_2
"attribute_1": "value",
"attribute_2": "someValue"
},
{ //item_3
"attribute_1": "value",
"attribute_2": "someValue"
}
]
}
But then I enabled CORS (keeping all options default), deployed the API with testing stage 'beta' and finally called it in a web app using jQuery's $.get() method. I got following response with empty "tableItems" array:
{
"tableItems": []
}
Suspecting something wrong in 'Method Response' section of API Gateway, I checked the 'Response Body for 200 ' settings. The content type was set to 'application/json' with models 'Empty'. Apparently, I can't change the latter values.
Update:
To drill down on the root problem, I enabled the Cloudwatch logs for Api Gateway, and invoke the REST API GET endpoint as follows:
$.get("https://my_api_id.execute-api_region.amazonaws.com/beta/get-items/{tablename}", {
tablename: "abc"
})
.then(res => console.log(res))
.catch(err => console.error(err));
In logs I'm getting following info:
{
"requestId": "...",
"ip": "...",
"caller": "-",
"user": "-",
"requestTime": "21/Apr/2020:08:03:41 +0000",
"httpMethod": "GET",
"resourcePath": "/get-items/{tablename}",
"status": "200",
"protocol": "HTTP/1.1",
"responseLength": "44" <--- to probe
}