Module: @accessors-modules/http-client

Module for HTTP clients. A simple use of this module is to request a web page and print its contents, as illustrated by the following example:

   var httpClient = require('@accessors-modules/http-client');
   httpClient.get('http://accessors.org', function(message) {
       print(message.body);
   });
Both http and https are supported.

Version:
  • $$Id$$
Author:
  • Marten Lohstroh, Edward A. Lee, Elizabeth Osyk
Source:

Methods

(static) get(options, responseCallback)

Convenience method to issue an HTTP GET. This just calls request() and then calls end() on the object returned by request(). It returns the object returned by request() (an instance of ClientRequest). See request() for documentation of the arguments.

Parameters:
Name Type Description
options

The options.

responseCallback

The callback function to call with an instance of IncomingMessage, or with a null argument to signal an error.

Source:

(static) post(options, responseCallback)

Convenience method to issue an HTTP POST. This just calls request() and then calls end() on the object returned by request(). It returns the object returned by request() (an instance of ClientRequest). See request() for documentation of the arguments.

Parameters:
Name Type Description
options

The options.

responseCallback

The callback function to call with an instance of IncomingMessage, or with a null argument to signal an error.

Source:

(static) put(options, responseCallback)

Convenience method to issue an HTTP PUT. This just calls request() and then calls end() on the object returned by request(). It returns the object returned by request() (an instance of ClientRequest). See request() for documentation of the arguments.

Parameters:
Name Type Description
options

The options.

responseCallback

The callback function to call with an instance of IncomingMessage, or with a null argument to signal an error.

Source:

(static) request(options, responseCallback)

Issue an HTTP request and provide a callback function for responses. The callback is a function that is passed an instance of IncomingMessage, defined here. This function returns an instance of ClientRequest, also defined here. The HTTP request will not actually be issued until you call the end() function on the returned ClientRequest.

The options argument is an object with the following fields. The REST accessor takes care of creating this options object for cases where only a string URL is provided. The url field is required. Defaults are provided for other fields if not present.

  • body: The request body, if any. This supports at least strings and image data.
  • headers: An object containing request headers. By default this is an empty object. Items may have a value that is an array of values, for headers with more than one value.
  • keepAlive: A boolean that specified whether to keep sockets around in a pool to be used by other requests in the future. This defaults to false.
  • method: A string specifying the HTTP request method. This defaults to 'GET', but can also be 'PUT', 'POST', 'DELETE', etc.
  • outputCompleteResponseOnly: If false, then the multiple invocations of the callback may be invoked for each request. This defaults to true, in which case there will be only one invocation of the callback.
  • timeout: The amount of time (in milliseconds) to wait for a response before triggering a null response and an error. This defaults to 5000.
  • url: A string that can be parsed as a URL, or an object containing the following fields:
    • host: A string giving the domain name or IP address of the server to issue the request to. This defaults to 'localhost'.
    • path: Request path as a string. This defaults to '/'. This can include a query string, e.g. '/index.html?page=12', or the query string can be specified as a separate field (see below). An exception is thrown if the request path contains illegal characters.
    • protocol: The protocol. This is a string that defaults to 'http'.
    • port: Port of remote server. This defaults to 80.
    • query: A query string to be appended to the path, such as '?page=12'.

Parameters:
Name Type Description
options

The options or URL.

responseCallback

The callback function to call with an instance of IncomingMessage, or with a null argument to signal an error.

Source:
Returns:

An instance of ClientRequest.