AWS API Gateway Payload Handling

Amazon API Gateway is an AWS service that enables you to maintain REST and WebSocket APIs at any scale. You can create APIs for use in your own applications, or you can make your APIs available to third-party app developers. The API Gateway allows access to data stored in the AWS Cloud, as well as other web services.

When attempting to perform an HTTP /Post request to AWS API Gateway a few things need to be considered. Assuming you enabled CORS correctly there still may be issues handling payloads. If the clients JS object is in the querystring url it needs to leverage JSON.Stringify() otherwise the event handler won't recognize the object, you will see [object object] since the event makes reference of this object, meaning you cant call JSON.Stringify() later within the lambda to resolve your object.

The problem here is, when you stringify your querystring the API Gateway will never get the request due to Integration mapping templates. The API Gateway querystring expects object values not entire objects themselves. When the conditions are not met you will see no logs for your lambda.

#set($allParams = $input.params())

{

  "params" : {

    #foreach($type in $allParams.keySet())

    #set($params = $allParams.get($type))

    "$type" : {

      #foreach($paramName in $params.keySet())

      "$paramName" : "$util.escapeJavaScript($params.get($paramName))"

      #if($foreach.hasNext),#end

      #end

    }

    #if($foreach.hasNext),#end

    #end

  }
}

The above snippet will group your querystrings and any additional params from your axios method into a params.querystring object, this object can have a JS object and or strings.

return new Promise((resolve, reject) => {
  axios({
    method: 'post',
    url: `${endpoint}/phasedirection?deviceID=${payload.phaseDirectionData.deviceID}&phaseDirection`,
    params: {
      phaseDirection: payload.phaseDirectionData
    },
      headers: headers
  })
  .then(response => {
    resolve(response.data);
  })
  .catch(err => {
    reject(`Error posting phase direction data: ${err}`)
  });
});

Finally when the request is made to the API Gateway it will resolve the data to the lambda function which will take the event object and allow you to manipulate the data however you see fit.

const returnedHandler = (event, context, callback) => {

    console.log('Received event:', event);
    console.log("Request received stringify: \n", JSON.stringify(event));

    console.log("Conext received stringify: \n", JSON.stringify(context));

    var param = event.params.querystring;

    var deviceID = param.deviceID;

    var dataObj = param.phaseDirection;

    console.log('dev&obj ', deviceID, dataObj);
}

As you can see, the lambda function takes the event.params.querystring and assigns it to the variables we need to get the job done.

I hope this helps you understand why interacting with the API Gateway needs a proper mapping template and also specific payload parameters on the client side.

Sort:  

Congratulations @embeddednature! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made your First Vote

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

Support SteemitBoard's project! Vote for its witness and get one more award!

Hello @embeddednature! This is a friendly reminder that you have 3000 Partiko Points unclaimed in your Partiko account!

Partiko is a fast and beautiful mobile app for Steem, and it’s the most popular Steem mobile app out there! Download Partiko using the link below and login using SteemConnect to claim your 3000 Partiko points! You can easily convert them into Steem token!

https://partiko.app/referral/partiko

Congratulations @embeddednature! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.028
BTC 54370.47
ETH 2283.51
USDT 1.00
SBD 2.33