JavaScript Facebook messenger API incomplete content in request body

The explanation is a bit long so please bear with me.

I am building a Facebook messenger bot which uses my sails.js/node.js server in the backend and a MongoDB database.

In my sails app, I have applied policies to the method of the controller which handles the operations to be performed after recieving a text from the user. In this policy, I am following the documentation(https://developers.facebook.com/docs/messenger-platform/webhook-reference - "Security" section) and comparing the x-hub-signature that comes in the request's header with the sha1 digest of the request payload(body).

So now whenever I am sending a message to the bot, it says in the policy that the signature from the request and the signature calculated by me is different and thus, doesnt go further. I double checked the app secret which I should use while calculating the digest and it seems to be correct. Another difference which I found was that, Facebook request also sends a "content-length" field in its header, which is different than character length of the body they sent in the same request. And this is what I think is the reason for different signatures but I am unable to resolve it and get to the root of the problem as to why is this happening.

Also another thing to note is that the same code that throws this mismatch error, runs perfectly at certain times(actually, most of the times).

So can somebody please help me this? I ll be forever grateful :)

Here is the code from the policy

var crypto = require('crypto');
if(req.headers['x-hub-signature']){
    //console.log('req headers -----', JSON.stringify(req.headers));
    //console.log('req body -----', JSON.stringify(req.body));

    var hmac, calculatedSignature, payload = req.body;
    hmac = crypto.createHmac('sha1', app_secret);
    hmac.update(JSON.stringify(payload));
    calculatedSignature = 'sha1='+hmac.digest('hex');

    //console.log("signature calculatedSignature",calculatedSignature);
    if(calculatedSignature === req.headers['x-hub-signature']){
        return next();
    }else{
        res.forbidden('You shall not pass!');
    }
}

This is a sample request header -

{"host":"e93d4245id.ngrok.io","accept":"*/*","accept-encoding":"deflate, gzip","content-type":"application/json","x-hub-signature":"sha1=d0cd8177add9b1ff367d411942603b0d08183964","content-length":"274","x-forwarded-proto":"https","x-forwarded-for":"127.0.0.1"}

And this is the body from the same request -

{"object":"page","entry":[{"id":"1778585282425767","time":1479476014038,"messaging":[{"sender":{"id":"userId"},"recipient":{"id":"recipientId"},"timestamp":1479468097895,"message":{"mid":"mid.1479468097895:efdc7d2c68","seq":2355,"text":"Hahahaha"}}]}]}
Answer:1

Currently, the string is Package A (123 queries) Package B (212 queries) Is it possible to use jquery to split it into new line like Package A (123 queries) Package B (212 queries) I ...

Currently, the string is Package A (123 queries) Package B (212 queries) Is it possible to use jquery to split it into new line like Package A (123 queries) Package B (212 queries) I ...

  1. jquery split string into array
  2. jquery split string by space
  3. jquery split string into array by comma
  4. jquery split string by pipe
  5. jquery split string by slash
  6. jquery split string by new line
  7. jquery split string into two variables
  8. jquery split string to array foreach
  9. jquery split string after character
  10. jquery split string by multiple characters
  11. jquery split string to integer array
  12. jquery split string by backslash
  13. jquery split string after number of characters
  14. jquery split string by dash
  15. jquery split string by length
  16. jquery split string by word
  17. jquery split string get last element
  18. jquery split string by string
  19. jquery split string by hyphen
  20. jquery split string by dot

I am unable to update the geodata array inside places.success function. console.log prints JSON object inside function, however outside function it's undefined. I want to access geodata array ...

I am unable to update the geodata array inside places.success function. console.log prints JSON object inside function, however outside function it's undefined. I want to access geodata array ...

  1. data saving service

I have an array named data as shown below and an array of objects named obj1. var data = ["004", "456", "333", "555"]; obj1 = [{ id: "004", name: "Rick", Active: "false" }, { id: "005", ...

I have an array named data as shown below and an array of objects named obj1. var data = ["004", "456", "333", "555"]; obj1 = [{ id: "004", name: "Rick", Active: "false" }, { id: "005", ...

How can i solve the issue of scope of variable in the below case. Myapp.controller('MyController', ['$scope', function ($scope) { var formvalues = {}; $scope.somebuttonclick = function()...

How can i solve the issue of scope of variable in the below case. Myapp.controller('MyController', ['$scope', function ($scope) { var formvalues = {}; $scope.somebuttonclick = function()...

  1. variable scope javascript issue