1 Answer
- Newest
- Most votes
- Most comments
0
Based off of this reference to the CDK interface MethodOptions, I believe you could update the mapping template of the OPTIONS by using the addMethod function on the resource and specifying the OPTIONS method. In the integration parameter of this function, you can specify the requestTemplates property to define the mapping templates for the OPTIONS method. Here's an example:
contentResource.addMethod('OPTIONS', new apigateway.MockIntegration({
integrationResponses: [{
statusCode: '200',
responseParameters: {
'method.response.header.Access-Control-Allow-Headers': "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
'method.response.header.Access-Control-Allow-Origin': "'*'",
'method.response.header.Access-Control-Allow-Methods': "'OPTIONS,GET,PUT'"
}
}],
passthroughBehavior: apigateway.PassthroughBehavior.NEVER,
requestTemplates: {
"application/json": "{\"statusCode\": 200}"
}
}), {
methodResponses: [{
statusCode: '200',
responseParameters: {
'method.response.header.Access-Control-Allow-Headers': true,
'method.response.header.Access-Control-Allow-Methods': true,
'method.response.header.Access-Control-Allow-Origin': true,
}
}]
});
Hope this helps!
-Zac
answered 10 months ago
Relevant content
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
Hello Zac
Thanks for your response. I have tried this approach before, however when I build the app using
npm run build
and then runcdk synth
, this throws errorError: There is already a Construct with name 'OPTIONS' in Resource [{content}]
. It seems there is already anOPTIONS
method defined by default.Do you think removing the
defaultCorsPreflightOptions
in RestApi definition and then adding this sectionaddMethod('OPTIONS',...
would work?