How to access header values in AWS lambda function

2019-07-28 08:44发布

问题:

I have created a Lambda authentication function in nodeJS and created an API gateway and called the function successfully by passing the parameters from body of the CURL.

Code.

var AWS = require('aws-sdk');
var lambda = new AWS.Lambda();
var RES;
exports.handler = function(event,context,callback) {
    var userName=event.userName;
    var passWord=event.passWord;
  var params = {
    FunctionName: 'ServiceAuthentication', // the lambda function we are going to invoke
    InvocationType: 'RequestResponse',
    LogType: 'Tail',
    Payload: '{ "userName": "'+userName+'","passWord": "'+passWord+'" }'
  };

  lambda.invoke(params, function(err, data) {
    if (err) {
      context.fail(err);

    } else {
      //context.succeed('ServiceAuthentication said '+data.Payload);
      var response=data.Payload;
      console.log("Response received is : ",response);
      if(response === true){
           RES = '{"ResponseJSON":{"Body":{"Datalist":{"Authentication":'+response+'}}}}';
      }else{
           RES = '{"ResponseJSON":{"Body":{"Datalist":{"Authentication":'+response+'}}}}'; 
      }
     callback(null, JSON.parse(RES));
    }});
};

Here in the above code am passing the username and password in the body in CURL request and received successful response

CURL Request:

curl -X POST -d '{"userName": "vikash|214057357158656","passWord": "12345"}' https://execute-api.us-west-2.amazonaws.com/prod/Invokefunction

Response:

{"ResponseJSON":{"Body":{"Datalist":{"Authentication":"true"}}}}

I want to pass the username and password in the header of the curl not in the body.

Ex:

curl -X POST -H "userName": "vikash|214057357158656" -H "passWord": "123456" -d '{}' https://execute-api.us-west-2.amazonaws.com/prod/Invokefunction

But lambda function does not access the header in the curl, how can this be done and how can we successfully pass the header values in the lambda function can any one help..