# GatewayResponseOptions

Struct · Stable

Options to add gateway response.

Used as argument in: [RestApiBase.addGatewayResponse](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.RestApiBase.html#addgatewayresponse)

- Source: [`aws-apigateway/lib/gateway-response.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-apigateway/lib/gateway-response.ts#L35)

## Example

TypeScript:

```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:

```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:

```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());
```

C#:

```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:

```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

- `responseHeaders`: { [key: string]: string } — Custom headers parameters for response.

  **Default:** no headers

- `statusCode`: string — Http status code for response.

  **Default:** standard http status code for the response type.

- `templates`: { [key: string]: string } — Custom templates to get mapped as response.

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

- `type`: [ResponseType](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.ResponseType.html) (required) — Response type to associate with gateway response.
