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.
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) {
},
});
The HTTP method or "verb".
'GET'
The URL where the request should be sent.
'/7/friends/search'
Optional base URL to resolve the main URL against, with or without the protocol prefix (e.g. "http://").
'api.example.com/pets'
Optional dictionary of data to encode and append in the URL's query string. Particularly useful for GET and DELETE requests.
{}
Optional data to encode and include in the request body. Designed for POST, PUT, and PATCH requests.
'==='
Custom format to use when encoding data for the request body.
'application/json'
Custom headers to include in the request (e.g. {"X-Auth": "k3yboardc4t"}).
{}
An unexpected error occurred.
A non-2xx status code was returned from the server.
{
statusCode: 404,
headers: {},
body: '...[{"maybe some JSON": "like this"}] (but could be any string)'
}
Unexpected connection error: could not send or receive HTTP request.
OK.
{
statusCode: 201,
headers: {},
body: '[{"maybe some JSON": "like this"}] (but could be any string)'
}