0

I am in the process of implementing a webhook for Mailgun on my node backend. Having trouble with processing the parameters. The only way I could read the parameters is by parsing the body, in which case the server looks like this:

var server = http.createServer(function (req, res) {
 var body = "";
 if (req.method == 'POST') {
   req.on('data', function (data) {
     body += data;
   });    
   req.on('end', function(data) {
     body += data;
     console.log("response: " + body);      
   });
}).listen(8080);

The output body is then formatted something like this:

(...)
--5bb8ffb8-e325-492c-b789-3d807dc78e74
Content-Disposition: form-data; name="Message-Id"

<20130503182626.18666.16540@sandbox94d44395c5264485ab775855eb8ff309.mailgun.org>
--5bb8ffb8-e325-492c-b789-3d807dc78e74
Content-Disposition: form-data; name="X-Mailgun-Sid"

WyIwNzI5MCIsICJhbGljZUBleGFtcGxlLmNvbSIsICI2Il0=
--5bb8ffb8-e325-492c-b789-3d807dc78e74
Content-Disposition: form-data; name="attachment-count"

1

Is there any way I could parse the body to get it to a json format? Or if not can I get the parameters in another way? I saw examples for other programming languages, but sadly the information could not be used in my case.

Vee6
  • 1,527
  • 3
  • 21
  • 40

1 Answers1

1

If you were to use ExpressJS, I would use body-parse - it simplifies the parsing of POST requests a lot.

...
app.post('/endpoint', function(req, res){
  var RESOURCE = req.body.RESOURCE;
...
});

When a POST is sent to endpoint you can parse the request's resource to make it do things!

Please do let me know if this is working for you! If not we can look at trying something else,

Kind regards,

API_sheriff_orlie
  • 1,223
  • 10
  • 18
  • Thank you for the answer. I actually used a node module called 'formidable' and it did worked really well for me: https://github.com/felixge/node-formidable – Vee6 Sep 25 '14 at 07:27