Skip to content
v2.278.0

CorsOptions ​

Struct Stable

Example ​

ts
new apigateway.RestApi(this, 'api', {
  defaultCorsPreflightOptions: {
    allowOrigins: apigateway.Cors.ALL_ORIGINS,
    allowMethods: apigateway.Cors.ALL_METHODS // this is also the default
  }
})
python
apigateway.RestApi(self, "api",
    default_cors_preflight_options=apigateway.CorsOptions(
        allow_origins=apigateway.Cors.ALL_ORIGINS,
        allow_methods=apigateway.Cors.ALL_METHODS
    )
)
java
RestApi.Builder.create(this, "api")
        .defaultCorsPreflightOptions(CorsOptions.builder()
                .allowOrigins(Cors.ALL_ORIGINS)
                .allowMethods(Cors.ALL_METHODS)
                .build())
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
new RestApi(this, "api", new RestApiProps {
    DefaultCorsPreflightOptions = new CorsOptions {
        AllowOrigins = Cors.ALL_ORIGINS,
        AllowMethods = Cors.ALL_METHODS
    }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
apigateway.NewRestApi(this, jsii.String("api"), &RestApiProps{
	DefaultCorsPreflightOptions: &CorsOptions{
		AllowOrigins: apigateway.Cors_ALL_ORIGINS(),
		AllowMethods: apigateway.Cors_ALL_METHODS(),
	},
})

Properties ​

allowCredentialsoptionalbooleanallow_credentialsoptionalboolallowCredentialsoptionalBooleanAllowCredentialsoptionalboolAllowCredentialsoptional*bool = false #
The Access-Control-Allow-Credentials response header tells browsers whether to expose the response to frontend JavaScript code when the request's credentials mode (Request.credentials) is "include".
notes

When a request's credentials mode (Request.credentials) is "include", browsers will only expose the response to frontend JavaScript code if the Access-Control-Allow-Credentials value is true.

Credentials are cookies, authorization headers or TLS client certificates.

allowHeadersoptionalstring[]allow_headersoptionaltyping.List[str]allowHeadersoptionalList<String>AllowHeadersoptionalstring[]AllowHeadersoptional*[]*string = Cors.DEFAULT_HEADERS #
The Access-Control-Allow-Headers response header is used in response to a preflight request which includes the Access-Control-Request-Headers to indicate which HTTP headers can be used during the actual request.
allowMethodsoptionalstring[]allow_methodsoptionaltyping.List[str]allowMethodsoptionalList<String>AllowMethodsoptionalstring[]AllowMethodsoptional*[]*string = Cors.ALL_METHODS #
The Access-Control-Allow-Methods response header specifies the method or methods allowed when accessing the resource in response to a preflight request.

If ANY is specified, it will be expanded to Cors.ALL_METHODS.

allowOriginsstring[]allow_originstyping.List[str]allowOriginsList<String>AllowOriginsstring[]AllowOrigins*[]*string required #
Specifies the list of origins that are allowed to make requests to this resource.
notes

If you wish to allow all origins, specify Cors.ALL_ORIGINS or [ * ].

Responses will include the Access-Control-Allow-Origin response header. If specific origins are specified (not Cors.ALL_ORIGINS), the Vary: Origin response header will also be included.

disableCacheoptionalbooleandisable_cacheoptionalbooldisableCacheoptionalBooleanDisableCacheoptionalboolDisableCacheoptional*bool #
Sets Access-Control-Max-Age to -1, which means that caching is disabled.

Default: cache is enabled

This option cannot be used with maxAge.

exposeHeadersoptionalstring[]expose_headersoptionaltyping.List[str]exposeHeadersoptionalList<String>ExposeHeadersoptionalstring[]ExposeHeadersoptional*[]*string #
The Access-Control-Expose-Headers response header indicates which headers can be exposed as part of the response by listing their names.

Default: only the 6 CORS-safelisted response headers are exposed: Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma

If you want clients to be able to access other headers, you have to list them using the Access-Control-Expose-Headers header.

maxAgeoptionalDurationmax_ageoptionalDurationmaxAgeoptionalDurationMaxAgeoptionalDurationMaxAgeoptionalDuration #
The Access-Control-Max-Age response header indicates how long the results of a preflight request (that is the information contained in the Access-Control-Allow-Methods and Access-Control-Allow-Headers headers) can be cached.

Default: browser-specific (see reference)

To disable caching altogether use disableCache: true.

statusCodeoptionalnumberstatus_codeoptionalint | floatstatusCodeoptionalNumberStatusCodeoptionaldoubleStatusCodeoptional*float64 = 204 #
Specifies the response status code returned from the OPTIONS method.