Tuesday 6 June 2017

Why do I keep getting RequestId:

I have a lambda function where I am trying to verify the user token from google recaptcha on the client side. I see in my dev tools that it is returning the correct payload user-token but the lambda function is coming back with an error message like this:

errorMessage : "RequestId: 60e203f2-4acf-11e7-ac0f-673e9366824b Process exited before completing request"

My Lambda funciton is below.

var AWS = require('aws-sdk');
const https = require('https');
const googleRecapchaSecret = "<secret-key>";
/* 
  Sample response query string
  https://www.google.com/recaptcha/api/siteverify?secret=your_secret&response=response_string
*/
function validateRecaptcha(recaptchaResponse,  callback, error){
    var querystring = require('querystring');    
    var postData = querystring.stringify({secret : googleRecapchaSecret,response : recaptchaResponse});
    var options = {
       hostname: 'www.google.com',
       path: '/recaptcha/api/siteverify',
       method: 'POST',
       port: 443, 
       headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(postData)
        }
      
    };   
  
   var req = https.request(options, (res) => {
        res.on('data', (d) => {
             if (d === undefined){ error('undefined'); return}
             
             var response = JSON.parse(d);
             /*if(response.success === true){
                 callback('true');
             }else{
                 callback(response);
             }*/
            if(response.success === true){
                callback(null, response);
            }else{
                callback(response);
            }
        });
   });  
  
   req.write(postData);
   req.end();
   
   req.on('error', (e) => {
       //console.log(e);
       callback('false');
   });
   
}
module.exports.grouveReCaptcha =  (event, context, callback) => {
    data = event;
//     if(data.body === undefined) { context.fail('Must provide parameters!'); return; }
//     console.log('Received Event:',data.body);
    
    
//   if(recaptchaResponse === undefined){ 
//         context.fail('Recaptcha cannot be validated!'); 
//         return;
//     }
    //var recaptchaResponse = event.body.data;
    
    var recaptchaResponse = body.data;
    console.log(recaptchaResponse);
    validateRecaptcha(recaptchaResponse, function(err, data){      
        if (err) {
            console.log("ReCAPTCHA verification failed!");
                callback(null, {'error': false, 'message': "You appear to be a robot!"});
                            context.done(err, null);
        } else {
            console.log("ReCAPTCHA verification succeeded!");
            context.done(null, data);
        }
    });
  
};

Now I have the timeout for the function was initially set at 10 seconds.Then I tried 20, and then 30 seconds but none of that works.



via Amen Ra

No comments:

Post a Comment