Soru

Zorluk: KolayTroubleshooting API Gateway Errors and CORS

A client-side web application hosted on `https://portal.example.com` attempts to send a `DELETE` request to a resource on a REST API hosted on Amazon API Gateway. The API utilizes a Lambda Proxy Integration. The browser console displays an error stating that the request has been blocked by CORS policy because no 'Access-Control-Allow-Origin' header is present on the requested resource.

Which TWO actions must the developer take to resolve this issue?

  1. Enable CORS on the resource in the Amazon API Gateway console to configure the OPTIONS method.Cevap
  2. Update the backend Lambda function response to include the Access-Control-Allow-Origin header.Cevap
  3. C
    Configure a CORS configuration rule on the Amazon S3 bucket hosting the web application to allow requests from the API Gateway domain.
  4. D
    Configure the API Gateway integration to return a raw text response rather than a structured JSON payload.
  5. E
    Create an API Gateway custom Lambda Authorizer that intercepts the preflight request and returns a policy allowing the origin.

Cevap

Enable CORS on the resource in the Amazon API Gateway console to configure the OPTIONS method, and update the backend Lambda function response to include the Access-Control-Allow-Origin header.
Resolving a CORS issue for a DELETE request requires two configuration steps. First, configuring the OPTIONS preflight method in API Gateway ensures that preflight CORS verification passes. Second, because Lambda Proxy Integration is utilized, the Lambda function itself must return the Access-Control-Allow-Origin header in its response envelope.

Adım Adım Çözüm

1
Configure the preflight response in API Gateway.
The OPTIONS method is enabled for the resource, returning the CORS headers needed to pass the initial preflight check.
Before sending a DELETE request, the browser performs a preflight OPTIONS check to verify if the destination server allows cross-origin requests.
2
Inject CORS headers in the Lambda proxy response.
The Lambda function returns a JSON response containing 'headers': {'Access-Control-Allow-Origin': 'https://portal.example.com'}.
Under Lambda Proxy Integration, API Gateway does not alter the backend response to add CORS headers, so the Lambda function must return them explicitly.

Anahtar Kavram

Handling CORS with Lambda Proxy Integrations requires configuring both the preflight OPTIONS method in API Gateway and the custom response headers inside the backend Lambda code.
Tahmini Süre:1m 30s
Bu soruyu puanla