Question

Difficulty: MediumTroubleshooting API Gateway Errors and CORS

A CORS preflight blocked error is displayed in the browser console when a client-side SvelteKit application hosted on https://manager.fleet-ops.net sends a POST request to an Amazon API Gateway REST API. The request includes a custom HTTP header named X-Client-Session-ID. The developer had previously enabled CORS on the API Gateway resource, which created an OPTIONS method returning the standard headers Access-Control-Allow-Origin and Access-Control-Allow-Methods. Which action must the developer take to resolve this CORS validation error?

  1. A
    Add the X-Client-Session-ID header to the CORS configuration policy of the Amazon S3 bucket hosting the frontend application.
  2. Update the API Gateway OPTIONS method integration response to include X-Client-Session-ID in the Access-Control-Allow-Headers header value, and redeploy the API.Answer
  3. C
    Modify the backend Lambda function associated with the POST method to return the Access-Control-Allow-Headers header containing X-Client-Session-ID in its response payload.
  4. D
    Configure the OPTIONS method in API Gateway to require an API Key and pass the X-Client-Session-ID as the API Key value in the request.

Answer

Update the OPTIONS method integration response in API Gateway to include the custom header in the Access-Control-Allow-Headers list, then deploy the API.
When a client application includes a custom HTTP header such as X-Client-Session-ID, the browser automatically sends a preflight OPTIONS request before the actual POST request. The OPTIONS method is typically configured in API Gateway using a Mock integration. To allow the request to proceed, the OPTIONS method's integration response must include the custom header name in its Access-Control-Allow-Headers value. The API must then be redeployed to apply the configuration change.

Step-by-Step Solution

1
Identify the stage of the failure.
The failure occurs during the preflight (OPTIONS) request, before the actual POST request is sent.
Since the client application includes a custom header, the browser initiates a preflight request which must pass CORS validation first.
2
Identify the required header parameter for custom headers.
The response to the preflight OPTIONS request must include the Access-Control-Allow-Headers header containing the name of the custom header.
Browsers reject requests with custom headers unless the destination server explicitly lists those headers as allowed.
3
Apply the configuration change and deploy.
Add the custom header to the OPTIONS method integration response in API Gateway and deploy the API to push changes to the active stage.
Changes made to the API Gateway configuration do not take effect until the API is deployed to a stage.

Key Concept

CORS preflight request handling with custom headers in API Gateway
Rate this question