A React Single Page Application (SPA) hosted on `https://portal.dev-ops-metrics.net` attempts to retrieve project status reports by sending an HTTP `GET` request to an Amazon API Gateway REST API. The API uses a Lambda proxy integration. Although the Lambda function executes successfully and returns a payload, the client application receives an HTTP `502 Bad Gateway` error with a response body of `{"message": "Internal server error"}`. The API Gateway CloudWatch execution logs display: `Execution failed due to configuration error: Malformed Lambda proxy response`. Which modification to the Lambda function's return payload will resolve this error?
- AReturn a JSON object containing the `status` key with a numeric value and the `payload` key containing the raw data structure.
- BReturn the raw data structure directly from the handler function, as API Gateway automatically wraps it in a standard HTTP response.
- Return a JSON object containing the `statusCode` key with an integer value and the `body` key containing a stringified JSON representation of the data.Cevap
- DReturn a JSON object containing the `statusCode` key and a `headers` key containing the `Access-Control-Allow-Origin` header set to `*`, omitting the `body` key.
Cevap
The Lambda function must return a JSON object containing the `statusCode` key with an integer value and the `body` key containing a stringified JSON representation of the data.
In an Amazon API Gateway REST API with Lambda Proxy integration, the backend Lambda function is responsible for constructing the complete HTTP response. The response returned by the Lambda function must be a JSON object (or dictionary) with specific keys, including `statusCode` (which must be an integer or string representing one) and `body` (which must be a string, often a stringified JSON object). Returning a response in this exact format allows API Gateway to successfully parse the result and return a valid HTTP response to the client application.
Adım Adım Çözüm
Anahtar Kavram
Lambda Proxy Integration Response Format
Tahmini Süre:1m 30s