A web application hosted on `https://manager.fleet-operations.com` sends an HTTP `POST` request to an Amazon API Gateway REST API. The API is integrated with a backend AWS Lambda function using a Lambda Proxy integration. Although the developer enabled CORS on the API Gateway resource, the browser console displays a CORS preflight blocked error and a `502 Bad Gateway` status. Which two actions must the developer take to resolve these errors?
- Modify the Lambda function response to include the 'Access-Control-Allow-Origin' header with the value 'https://manager.fleet-operations.com'.Cevap
- Ensure the Lambda function returns a structured JSON object containing the 'statusCode', 'headers', and 'body' fields.Cevap
- CAdd a CORS configuration policy to the Amazon S3 bucket that hosts the frontend application at 'https://manager.fleet-operations.com'.
- DConfigure an API Gateway integration response mapping template to extract the headers and format the response.
- EChange the API Gateway integration type to Lambda Custom integration and configure the Lambda function to return a plain text string instead of a JSON object.
Cevap
To resolve the CORS and 502 Bad Gateway errors, the developer must modify the Lambda function response to include the 'Access-Control-Allow-Origin' header with the value 'https://manager.fleet-operations.com', and ensure the Lambda function returns a structured JSON object containing the 'statusCode', 'headers', and 'body' fields.
The correct options are to modify the Lambda function response to include the 'Access-Control-Allow-Origin' header with the client's origin, and ensure the Lambda function returns a structured JSON object containing 'statusCode', 'headers', and 'body'. In a Lambda Proxy integration, the backend Lambda function is responsible for both the formatting of the response payload (which prevents the 502 Bad Gateway error) and returning the necessary CORS headers.
Adım Adım Çözüm
Anahtar Kavram
CORS and Response Formatting in API Gateway Lambda Proxy Integrations