Skip to content
v2.278.0

GatewayResponseOptions Struct ​

Options to add gateway response.

Example ​

ts
const api = new apigateway.RestApi(this, 'books-api');
api.addGatewayResponse('test-response', {
  type: apigateway.ResponseType.ACCESS_DENIED,
  statusCode: '500',
  responseHeaders: {
    // Note that values must be enclosed within a pair of single quotes
    'Access-Control-Allow-Origin': "'test.com'",
    'test-key': "'test-value'",
  },
  templates: {
    'application/json': '{ "message": $context.error.messageString, "statusCode": "488", "type": "$context.error.responseType" }'
  }
});
python
api = apigateway.RestApi(self, "books-api")
api.add_gateway_response("test-response",
    type=apigateway.ResponseType.ACCESS_DENIED,
    status_code="500",
    response_headers={
        # Note that values must be enclosed within a pair of single quotes
        "Access-Control-Allow-Origin": "'test.com'",
        "test-key": "'test-value'"
    },
    templates={
        "application/json": "{ \"message\": $context.error.messageString, \"statusCode\": \"488\", \"type\": \"$context.error.responseType\" }"
    }
)
java
RestApi api = new RestApi(this, "books-api");
api.addGatewayResponse("test-response", GatewayResponseOptions.builder()
        .type(ResponseType.ACCESS_DENIED)
        .statusCode("500")
        .responseHeaders(Map.of(
                // Note that values must be enclosed within a pair of single quotes
                "Access-Control-Allow-Origin", "'test.com'",
                "test-key", "'test-value'"))
        .templates(Map.of(
                "application/json", "{ \"message\": $context.error.messageString, \"statusCode\": \"488\", \"type\": \"$context.error.responseType\" }"))
        .build());
csharp
// Install: dotnet add package Amazon.CDK.Lib
var api = new RestApi(this, "books-api");
api.AddGatewayResponse("test-response", new GatewayResponseOptions {
    Type = ResponseType.ACCESS_DENIED,
    StatusCode = "500",
    ResponseHeaders = new Dictionary<string, string> {
        // Note that values must be enclosed within a pair of single quotes
        { "Access-Control-Allow-Origin", "'test.com'" },
        { "test-key", "'test-value'" }
    },
    Templates = new Dictionary<string, string> {
        { "application/json", "{ \"message\": $context.error.messageString, \"statusCode\": \"488\", \"type\": \"$context.error.responseType\" }" }
    }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
api := apigateway.NewRestApi(this, jsii.String("books-api"))
api.AddGatewayResponse(jsii.String("test-response"), &GatewayResponseOptions{
	Type: apigateway.ResponseType_ACCESS_DENIED(),
	StatusCode: jsii.String("500"),
	ResponseHeaders: map[string]*string{
		// Note that values must be enclosed within a pair of single quotes
		"Access-Control-Allow-Origin": jsii.String("'test.com'"),
		"test-key": jsii.String("'test-value'"),
	},
	Templates: map[string]*string{
		"application/json": jsii.String("{ \"message\": $context.error.messageString, \"statusCode\": \"488\", \"type\": \"$context.error.responseType\" }"),
	},
})

Properties ​

responseHeadersoptional{ [key: string]: string }response_headersoptionaltyping.Mapping[str, str]responseHeadersoptionalMap<String, String>ResponseHeadersoptionalIDictionary<string, string>ResponseHeadersoptional*map[string]*string #
Custom headers parameters for response.

Default: no headers

statusCodeoptionalstringstatus_codeoptionalstrstatusCodeoptionalStringStatusCodeoptionalstringStatusCodeoptional*string #
Http status code for response.

Default: standard http status code for the response type.

templatesoptional{ [key: string]: string }templatesoptionaltyping.Mapping[str, str]templatesoptionalMap<String, String>TemplatesoptionalIDictionary<string, string>Templatesoptional*map[string]*string #
Custom templates to get mapped as response.

Default: Response from api will be returned without applying any transformation.

Response type to associate with gateway response.