4

I have been experimenting with AWS API gateway service. I wanted to give default values to some of the parameters in the method request. However I couldn't find any option to do so. Is there any way to do that from the interface provided ?

5 Answers 5

4

You can set a static (i.e. a default/constant) value by using single quotes in the Integration Request mapping.

From the documentation at http://docs.aws.amazon.com/apigateway/latest/developerguide/request-response-data-mappings.html

The STATIC_VALUE is a string literal and must be enclosed within a pair of single quotes.

Sign up to request clarification or add additional context in comments.

1 Comment

i'm on this for the last week (spare/free time) but i cannot make it work. I'm using 'integration.request.querystring.customvalue': "'11111'" but customvalue is not passed to my lambda, i'm even using the lambda proxy integration to pass EVERYTHING to it and logging the incoming event, but there is no trace of customvalue
2

I took me a moment to find a good example of this, below are links to more documentation.

Doc Example:

Replace 'parameter-name' with the key and static-value with the value of your static key value pair to add the static param to the request headers.

"responseParameters": {
   "integration.request.header.parameter-name": "'static value'"
}

CDK Example

const lambdaIntegration = new apigateway.LambdaIntegration(lambdaEndpoint, {
   proxy: true,
   requestParameters: {
      "integration.request.header.parameter-name": "'static value'"
   }
});

1 Comment

why you use the responseParameters in the first example and the requestParameters in the second one?
1

No, API Gateway does not currently provide any direct support for default values. If your integration endpoint is a Lambda function, then coding your own default values if fairly straight forward. Otherwise, you might be able to implement default value logic in your integration request mapping template as long as the integration endpoint is expecting the parameters in the request body.

2 Comments

The integration endpoint is not expecting the parameters in the request body. So is it not possible to set default values in this case in the API Gateway ?
If the integration endpoint is not Lambda and the parameters are not in the request body, then API Gateway does not currently have any way to set default values for those parameters.
1

There is a way using Method Execution with mapping.

Say you have an optional parameter X:

Method Request > URL Query String Parameters : Add X as an optional

then

Integration Request > Mapping Templates > Add mapping template of type application/json

In the mapping field, set a new variable to match your input parameter (just do this on the first line:

#set($X = $input.params('X'))

and then add this to the place you need your parameter inside your JSON request

   #if(!$X || $X.length() == 0)
        "X": "null"
    #end
    #if($X && $X.length() != 0)
        "o": "$X"
    #end

Obviously you can modify the logic to suit you and instead of "null" use any default value you need.

Here's a full mapping example

#set($X = $input.params('X'))
{
     "somethingelse": "someothervalue",
     #if(!$X || $X.length() == 0)
            "X": "your default value here"
        #end
        #if($X && $X.length() != 0)
            "o": "$X"
        #end
     }
}

Comments

0

Similar to Andy's response, you can resolve the value outside the json body. Consider this example that sets page_size equals to 50 if not found:

#set($page_size = $input.params('page_size'))
#if($page_size != '')
  #set($page_size = 50)
#end
{
  "page_size": $page_size
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.