http

HTTP client.
Example
var http = require('http');
 var request = http.request({
    //alternative to host/port/path is
    //URL: 'http://localhost:888/getAppInfo',
    host: 'localhost', port: '80', path: '/getAppInfo',
    method: 'POST',
    sendTimeout: 30000, receiveTimeout: 30000,
    keepAlive: true,
    compressionEnable: true
 });
 request.write('Add string to response');
 var fileContent = fs.readFileSync('d:\binaryFile.txt'); // return ArrayBuffer, since encoding not passed
 request.write(fileContent, 'base64'); // write file content as base64 encoded string
 var response = request.end();


 var http = require('http');
 var assert = require('assert');
 var DOMParser = require('xmldom').DOMParser;
 // set global proxy settings
 http.setGlobalProxyConfiguration('proxy.main:3249', 'localhost');
 var resp = http.get('http://www.google.com/');
 assert.ok(resp.headers('via').startsWith('1.1 proxy.main'), 'proxy used');
 var index = resp.read();
 var doc = new DOMParser().parseFromString(index);
 assert.ok(doc.documentElement.textContent.startsWith('Google'), 'got Google from google');

Classes

ClientRequest
IncomingMessage

Members

STATUS_CODES: Object:.<number:, string:> static

HTTP status codes.

Methods

setGlobalProxyConfiguration(proxy, bypassopt) static

Configure global ( on the http module level) proxy server in case you can't configure it using either proxycfg.exe -u on Windows XP or netsh winhttp import proxy source=ie for other win version or by pass options.proxyName parameter.

Settings applied only for newly created {ClientRequest} instances.

See for details this MS article

Arguments:
  1. proxy (String)  name of the proxy server to use in format [[http|https]://]host[:port] For example 'http://proxy.my.domain:3249'
  2. [bypass] (String|Array)  semicolon delimited list jr array of host names or IP addresses, or host masks or both, that should not be routed through the proxy

setGlobalConnectionDefaults(defaults) static

Override global ( on the http module level) connectiuon defaults.

Settings applied only for newly created {ClientRequest} instances.

     var http = require('http');
     http.setGlobalConnectionDefaults({receiveTimeout: 60000}); // set receive timeout to 60 sec.
Arguments:
  1. defaults (Object)
    Properties
    1. [useHTTPS=false] (Boolean)
    2. [useCompression=true] (Boolean)  Send 'Accept-encoding: gzip' header to server & unzip zipper responses
    3. [keepAlive=false] (Boolean)  Use keep Alive HTTP protocol feature if server support it.
    4. [sendTimeout=30000] (Number)  Send timeout in ms.
    5. [receiveTimeout=30000] (Number)  Receive timeout in ms.
    6. [connectTimeout=60000] (Number)  Connect timeout in ms.

request(options) → ClientRequest static

Create new HTTP server connection. In case server behind the proxy - see http.setGlobalProxyConfiguration function.
Arguments:
  1. options (Object|String)  Either URL string in format protocol://host:port/path or config
    Properties
    1. [URL] (String)  Service URL in format protocol://host:port/path. Will override useHTTPS, server, host,portandpath` if passed
    2. [server] (String)  DEPRECATED. Server to connect in format 'host:port' or 'host' in case port == 80.
    3. [host] (String)  Host to connect. If server not specified this value used
    4. [port] (String)  Port. Default is 80 for HTTP or 443 for HTTPS
    5. [path='/'] (String)  Request path. Defaults to '/'. Should include query string if any. E.G. '/index.html?page=12'
    6. [method='GET'] (String)  HTTP method to use for request
    7. [headers] (Object:.<string:, string:>)  An object containing request headers
    8. [useHTTPS=false] (Boolean)
    9. [useCompression=true] (Boolean)  Send 'Accept-encoding: gzip' header to server & unzip zipper responses
    10. [keepAlive=false] (Boolean)  Use keep Alive HTTP protocol feature if server support it.
    11. [sendTimeout=30000] (Number)  Send timeout in ms.
    12. [receiveTimeout=30000] (Number)  Receive timeout in ms.
    13. [connectTimeout=30000] (Number)  Connect timeout in ms.

buildURL(url, params) → String static

Add parameters to URL

 http.buildURL('/myMethod', {a: 1, b: "1212"}; // '/myMethod?a=1&b=1212
Arguments:
  1. url (String)
  2. params (Object)

get(options, URLParamsopt) → IncomingMessage static

Since most requests are GET requests without bodies, we provides this convenience method. The two difference between this method and http.request() is that

  • it sets the method to GET and calls req.end() automatically
  • can optionally take URLParams Object {paramName: paramValue, ..} and add parameters to request path
Arguments:
  1. options (Object)  Request options as described in http.request
  2. [URLParams] (Object)  optional parameters to add to options.path