A developer has configured an Amazon API Gateway REST API with a Lambda Proxy Integration. A client application sends an HTTP POST request containing a JSON body with the following structure:
{
"email": "[email protected]",
"name": "John Doe"
}
In the Lambda function code, the developer attempts to retrieve the email using the following code:
javascript
const email = event.email;
However, the function execution logs show that the `email` variable is `undefined`. How should the developer modify the Lambda function code to correctly retrieve the email address?
- ARetrieve the email property from the request headers (for example: `const email = event.headers.email;`).
- Parse the body of the event as a JSON object, then access the email property (for example: `const email = JSON.parse(event.body).email;`).Cevap
- CAccess the email property directly from the query string parameters (for example: `const email = event.queryStringParameters.email;`).
- DAccess the email property from the request context object (for example: `const email = event.requestContext.email;`).
Cevap
Parse the body of the event as a JSON object, then access the email property (for example: `const email = JSON.parse(event.body).email;`).
Under API Gateway Lambda Proxy Integration, the request body is not automatically parsed by API Gateway. Instead, the raw string representation of the request payload is passed to the Lambda function in the `body` property of the `event` object. To access properties such as the email address, the developer must first deserialize the JSON string using a method like `JSON.parse(event.body)` before accessing the specific field.
Adım Adım Çözüm
Anahtar Kavram
API Gateway Lambda Proxy Integration payload format
Tahmini Süre:1m 30s