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
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.
1 Comment
'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 customvalueI 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
responseParameters in the first example and the requestParameters in the second one?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
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
}
}