简体   繁体   中英

Different results when I test serverless code in Lambda via AWS Console vs Curl

I have followed this tutorial: https://www.serverless.com/blog/node-rest-api-with-serverless-lambda-and-dynamodb . It guides you on building a Lambda function that takes three parameters and populates a DynamoDB database with an Item built from these parameters.

Everything works as expected when I run things from my console using CURL. However, if I test things via the Lambda Console I get an error, and it seems to be due to the 'event' object in my handler's parameters being shaped differently depending on where I call the Lambda function from (which is weird).

I tested the following:

module.exports.submit = async (event, context) => {
  return {
    statusCode: 200,
    body: Object.keys(event).join()
  }
}

And using CURL with the following statement I get(personal details are from the Tutorial):

curl -H "Content-Type: application/json" -X POST -d '{"fullname":"Shekhar Gulati","email": "shekhargulati84@gmail.com", "experience":12}' https://__.execute-api.eu-west-2.amazonaws.com/dev/__

The response from this CURL statement is quite long

resource,path,httpMethod,headers,multiValueHeaders,queryStringParameters,multiValueQueryStringParameters,pathParameters,stageVariables,requestContext,body,isBase64Encoded

On the other hand, in the console, the response is simply:

firstname,email,experience

Should I just ignore the console for testing purposes or am I missing something?

When calling your endpoint using curl or Postman, it sends an HTTP request to API Gateway. API Gateway then creates a specific event object and passes that to the Lambda function. When you invoke the Lambda directly in the console you pass whatever event object you create manually. The way to test your Lambda function directly would be to take an example API Gateway event and modify it to fit what you are trying to test. you can do this by logging out the event object from the Lambda when called using an HTTP request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM