0
votes

I have an API within API Gateway and I want to enable or disable caching on request parameters via AWS SDK.

The method is GET /cats. I'm using the updateStage method and I have tried the following:

params = {
  restApiId: 'myRestApiId',
  stageName: 'myStageName',
  patchOperations: [
    {
      op: 'replace',
      path: '/~1cats/GET/requestParameters/method.request.header.pawId/caching/enabled'
    }];
await aws.updateStage(params).promise();

which fails with:

Invalid method setting path: requestParameters/method.request.path.pawId/caching/enabled. Must be one of: [.../metrics/enabled, .../logging/dataTrace, .../logging/loglevel,.../throttling/burstLimit, .../throttling/rateLimit, .../caching/ttlInSeconds, .../caching/enabled, .../caching/dataEncrypted, .../caching/requireAuthorizationForCacheControl, .../caching/unauthorizedCacheControlHeaderStrategy]

which is strange, because .../caching/enabled is one of the options it "must be"!

How do I enable caching on my request parameter via the SDK?

1

1 Answers

2
votes

My understanding is you cannot enable caching of requestParameters directly on staged API. You will need to update API and deploy it to stage again. The cachingEnabled option you are seeing at stage level is for enabling caching of full API responses(not parameters). https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-caching.html

enter image description here

Now for updating the API to enable caching of request Parameters can be done using following operation.

var params = {
  httpMethod: 'GET',
  resourceId: 'xxxx', /* you will need to pass unique identifier which API gateway creates for /cats or /pets resources */
  restApiId: 'xxxxxxx', /* unique identifer for your API */
  patchOperations: [
    {
      op: 'add', /* add or remove only for enabling/disabling */
      path: '/cacheKeyParameters/method.request.header.pawId',
    },
    /* more items */
  ]
};
apigateway.updateIntegration(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

Note - The path changes according to which location you are enabling caching of parameter. Example - method.request.header.pawId or integration.request.header.pawId etc.,

One of the easiest way to find out exact path and which methods need to use is by calling their respective getStage, getIntegration, getMethod first and studying responses.