HTTP.getStream()

Send a GET request and immediately return the stream, which can be pumped to obtain the bytes of the response.

This machine determines whether a URL can be reached via a GET request and whether it responds with a 2xx status code. If so, it waits until it's able to parse out response headers, then returns a Readable stream representing the response body. The responsibility for any other validations/error handling falls on the consumer of the stream. However, this machine _DOES_ bind an `error` event handler on the stream to prevent emitted error events from crashing the process; ensuring that this machine is agnostic of its userland environment. If you plan to write code which uses the readable stream returned by this machine but you have never worked with Readable streams in Node.js, [check this out](https://docs.nodejitsu.com/articles/advanced/streams/how-to-use-fs-create-read-stream) for tips. For more conceptual information about readable streams in Node.js in general, check out the section on [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable) in the Node.js docs.

Usage

Hover over inputs/exits for more info.

var HTTP = require('machinepack-http');


// Send a GET request and immediately return the stream, which can be pumped to obtain the bytes of the response.

HTTP.getStream({

url: '/7/friends/search',

baseUrl: 'api.example.com/pets',

data: {},

headers: {},

}).exec({

// An unexpected error occurred.

error: function (err) {

 

},

// A non-2xx status code was returned from the server.

non200Response: function (result) {

 

},

// Unexpected connection error: could not send or receive HTTP request.

requestFailed: function () {

 

},

// OK.

success: function (result) {

 

},

});

Inputs

url required

The URL where the request should be sent.

'/7/friends/search'
baseUrl

An optional base URL to resolve the main `url` against.

'api.example.com/pets'
data

Optional data to send with this request.

{}
headers

Headers to include in the request (e.g. {"Content-Type": "application/json"}).

{}

Exits

error

An unexpected error occurred.

non200Response

A non-2xx status code was returned from the server.

{ statusCode: 404, headers: {} }
requestFailed

Unexpected connection error: could not send or receive HTTP request.

success

OK.

'==='