NateHill
NateHill

Reputation: 355

How to access specific request parameter

I thought the format for Twilio's request parameters were being returned as JSON-serialized data --according to the documentation listed for The Response Callback section (http://twilio.github.io/twilio-node/#restResources)

If that is the case I thought I could access request parameters (specifically the body of the text) using JSON.parse as outlined below... however I'm not getting any type of response.

var http = require('http');
var twilio = require('twilio');

http.createServer(function (req, res) {

  var body = '';

  req.on('data', function(data) {
    var messageBody = JSON.parse(data);
    body += messageBody.body;
  });

  req.on('end', function() {
    //Create TwiML response
    var twiml = new twilio.TwimlResponse();

    twiml.message('Thanks, your message of "' + body + '" was received!');

    res.writeHead(200, {'Content-Type': 'text/xml'});
    res.end(twiml.toString());
    });

}).listen(8080);

Before that I had tried accessing the parameters as illustrated below... but the body text always returned "undefined".

var http = require('http');
var twilio = require('twilio');

http.createServer(function (req, res) {

  var body = '';

  req.on('data', function(data) {
    body += data.body;
  });

  req.on('end', function() {
    //Create TwiML response
    var twiml = new twilio.TwimlResponse();

    twiml.message('Thanks, your message of "' + body + '" was received!');

    res.writeHead(200, {'Content-Type': 'text/xml'});
    res.end(twiml.toString());
    });

}).listen(8080);

And if I just do body += data; then I received all of the request parameters grouped together -- which I don't need.

Thoughts? Could you explain how to access specific request parameters?

*********UPDATED********* After incorporating @hexacyanide's suggestion I ended up with the following code that works

var http = require('http');
var twilio = require('twilio');
var qs = require('querystring');

http.createServer(function (req, res) {

  var body = '';

  req.setEncoding('utf8');

  req.on('data', function(data) {
    body += data;
  });

  req.on('end', function() {
    var data = qs.parse(body);
    var twiml = new twilio.TwimlResponse();
    var jsonString = JSON.stringify(data);
    var jsonDataObject = JSON.parse(jsonString);
    twiml.message('Thanks, your message of ' + jsonDataObject.Body + ' was received!');
    res.writeHead(200, {'Content-Type': 'text/xml'});
    res.end(twiml.toString());
  });

}).listen(8080);

Upvotes: 0

Views: 644

Answers (1)

hexacyanide
hexacyanide

Reputation: 91679

As I answered in a similar question of yours, you must wait for the entire response before you can do anything with it. When you're receiving data from the request stream:

var buf = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
  buf += chunk;
});

You are receiving raw data. It is unprocessed bytes, it hasn't been parsed, and you don't know how much of the total data it represents (unless you've checked content length headers). That also means you can't use JSON.parse(), because you will be getting a chunk of data of unknown length that usually doesn't represent the entire JSON string (it could if it were just brackets, or was really short in length, etc).

Even when receiving specific request parameters, you must receive the entire request, and parse it. Modules such as bodyParser() from the popular module Express were created for this purpose. If you don't want to use a module, just collect the request, and parse it:

var buf = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
  buf += chunk;
});
req.on('end', function() {
  var data = JSON.parse(buf);
});

Upvotes: 2

Related Questions