HTTP.sendHttpRequest()

Send an HTTP request and receive the response.

The "Send custom HTTP request" machine underlies all HTTP requests sent by this pack. It is often unnecessary to call this machine directly, as several higher-level alternatives like "GET" and "POST" are available, and are easier to use. "Send custom HTTP request" provides more flexibility, and so it is a bit more complex. But if your use case demands less commonly-used options, this machine is for you. Internally, this machine uses the [`request` module](http://npmjs.com/package/request), a simplified HTTP request client for Node.js. For advanced usage that is outside the scope of this machine (e.g. `multiplart/related` encoding for .NET WebAPI v4.0), write a custom machine and use `require('request')` directly.

Usage

Hover over inputs/exits for more info.

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


// Send an HTTP request and receive the response.

HTTP.sendHttpRequest({

method: 'GET',

url: '/7/friends/search',

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

qs: {},

body: '===',

enctype: 'application/json',

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

method required

The HTTP method or "verb".

'GET'
url required

The URL where the request should be sent.

'/7/friends/search'
baseUrl

Optional base URL to resolve the main URL against, with or without the protocol prefix (e.g. "http://").

'api.example.com/pets'
qs

Optional dictionary of data to encode and append in the URL's query string. Particularly useful for GET and DELETE requests.

{}
body

Optional data to encode and include in the request body. Designed for POST, PUT, and PATCH requests.

'==='
enctype

Custom format to use when encoding data for the request body.

'application/json'
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: {},
  body: '...[{"maybe some JSON": "like this"}]  (but could be any string)' }
requestFailed

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

success

OK.

{ statusCode: 201,
  headers: {},
  body: '[{"maybe some JSON": "like this"}]  (but could be any string)' }