0
votes

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
}
2
Enable CloudWatch logs for your API and check what body is being sent to DynamoDB from API Gateway. It's possible the mapping template is sending empty body which leads to an empty response. docs.aws.amazon.com/apigateway/latest/developerguide/… - Suraj Bhatia
Thanks for your responding. I did that. Please check my updated question above. During testing on Api-gateway console, I get desired response. But in web app, it's returning empty array - Waleed93
Your API endpoint has the table name appended at the end.. but you also send the tablename as a parameter.. Is this correct? - Mulhoon
@Mulhoon, Hmm... But now it sends 403 forbidden error saying in response "Missing Authentication Token". - Waleed93
The logs you have added are access logs. The other type of logging - execution logs would be more helpful in this case. - Suraj Bhatia

2 Answers

0
votes

The whole problem is with the very url path /get-items/{tablename} being used.

The {tablename} is a symbolic representation of a query parameter value. It is where the value abc of tablename request-parameter should be added, and so the symbol must be replaced.

Hence, the correct API endpoint would be:

https://my_api_id.execute-api_region.amazonaws.com/beta/get-items/abc
0
votes

Just putting it out here for anyone who runs into similar issue -

There could be more than one possibility why the database was sending back an empty response. In this case, it looked like the request was not being sent completely from API Gateway as the DB expected. Best way to debug such issues would be to enable API Gateway Execution logs with full request/response option.

These logs would should you how the request was received at API Gateway from the client and how it was modified (by mapping templates) and forwarded to the integration (Database in this case).