A `502 Bad Gateway` error occurs when a locally running Electron desktop application sends an HTTP `POST` request to an Amazon API Gateway REST API. The developer also notices a CORS failure message in the application logs: 'Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin'. The API Gateway endpoint uses a Lambda proxy integration. The backend Lambda function's logs in Amazon CloudWatch show that the function completes successfully and returns the following structure:
{
"status": 200,
"body": {
"message": "Data processed successfully",
"itemId": "12345"
}
}
Which changes must the developer make to resolve both the `502 Bad Gateway` error and the CORS block? (Select TWO.)
- Modify the Lambda function's output to use the key 'statusCode' instead of 'status', and convert the JSON object in 'body' into a stringified JSON format.Cevap
- Add a 'headers' object containing the 'Access-Control-Allow-Origin' key set to 'http://localhost:8080' inside the Lambda function's returned JSON payload.Cevap
- CConfigure the API Gateway Integration Response for the POST method to inject the 'Access-Control-Allow-Origin' header.
- DApply a CORS policy to the Amazon S3 bucket that hosts the static assets of the desktop application.
- EChange the integration type from Lambda Proxy to HTTP Proxy and define a URL path parameter mapping for the backend.
Cevap
To resolve the issues, the developer must modify the Lambda function to return a correctly formatted JSON response with a 'statusCode' key and a stringified JSON 'body' to resolve the 502 Bad Gateway error. The developer must also add the 'Access-Control-Allow-Origin' header to the 'headers' map in the Lambda function's response to resolve the CORS block.
To fix the 502 Bad Gateway error under a Lambda proxy integration, the Lambda function's output must contain the 'statusCode' key (rather than 'status') and the 'body' must be stringified. To resolve the CORS failure, the response payload from the Lambda function must explicitly contain the CORS headers, such as 'Access-Control-Allow-Origin', in the 'headers' object.
Adım Adım Çözüm
Anahtar Kavram
Lambda Proxy Integration Response Format and CORS Configuration