1

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..

Nag
  • 357
  • 2
  • 9
  • 28
  • 2
    Possible duplicate of http://stackoverflow.com/questions/31372167/how-to-access-http-headers-for-request-to-aws-api-gateway-using-lambda – Paritosh Jul 19 '16 at 12:35
  • 1
    Your question is an exact duplicate of http://stackoverflow.com/questions/31372167/how-to-access-http-headers-for-request-to-aws-api-gateway-using-lambda I suggest using kennbrodhagen's answer to that question. – Mark B Jul 19 '16 at 13:04
  • Thanks you @MarkB kennbrodhagen's answer worked for me. – Nag Jul 20 '16 at 05:28

0 Answers0