Soru

Zorluk: OrtaServerless Development with AWS Lambda

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?

  1. A
    Retrieve the email property from the request headers (for example: `const email = event.headers.email;`).
  2. 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
  3. C
    Access the email property directly from the query string parameters (for example: `const email = event.queryStringParameters.email;`).
  4. D
    Access 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

1
Identify the API Gateway integration type being used.
The scenario specifies Lambda Proxy Integration.
Different integration types pass request data to the Lambda function in different formats.
2
Determine where the HTTP request body is located in the Lambda event object for Lambda Proxy Integration.
The HTTP body is passed as a stringified JSON payload in the `event.body` property.
API Gateway does not automatically parse the JSON body of a request into the root of the event object.
3
Parse the stringified body and access the target property.
Using `JSON.parse(event.body).email` correctly retrieves the value of the email key.
This deserializes the JSON string into an object so that its fields can be accessed programmatically.

Anahtar Kavram

API Gateway Lambda Proxy Integration payload format
Tahmini Süre:1m 30s
Bu soruyu puanla