herp

0.0.2 • Public • Published

herp - HTTP Slurp

This wraps the http.request method and takes care of writing request data to a stream, and reading response data from a stream. It takes request data, in string or buffer form, with a data parameter. It automatically calls end(). The callback is passed a response with the response data in data. It is always a string, read with the utf8 encoding. Unlike http.request where the callback is called with just one parameter, the response, the callback passed to herp takes an err parameter because the intended workflow doesn't involve using the EventEmitter's event listening directly.

GET example

Without herp:

var request = require('http').request;
var options = {
  host: 'jsonip.com',
  path: '/'
};
var req = request(options, function(res) {
  var data = '';
  res.on('data', function(chunk) {
    data += chunk;
  });
  res.on('end', function() {
    console.log('got ' + data);
  });
});
req.on('error', function(err) {
  console.log('got an error!');
});
req.end();

With herp:

var request = require('./herp');
var options = {
  host: 'jsonip.com',
  path: '/'
};
request(options, function(err, res) {
  if (err) console.log('got an error!');
  console.log('got ' + res.data);
});

POST example

TODO

License

MIT.

Readme

Keywords

Package Sidebar

Install

npm i herp

Weekly Downloads

1

Version

0.0.2

License

MIT

Last publish

Collaborators

  • bat-archive