How to access header values in AWS lambda function

别说谁变了你拦得住时间么 提交于 2019-12-11 06:18:02

问题


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

来源:https://stackoverflow.com/questions/38452587/how-to-access-header-values-in-aws-lambda-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!