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 if client is behind a proxy
// http.setGlobalProxyConfiguration('proxy.main:3249', 'localhost');
var resp = http.get('https://synopse.info/fossil/wiki/Synopse+OpenSource');
// check we are actually behind a proxy
// assert.ok(resp.headers('via').startsWith('1.1 proxy.main'), 'proxy used');
var index = resp.read();
console.log(index);
// var doc = new DOMParser().parseFromString(index);
// assert.ok(doc.documentElement.textContent.startsWith('mORMot'), 'got mORMot from mORMot');
Classes
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:
-
proxy
(String)
 name of the proxy server to use in format [[http|https]://]host[:port]
For example 'http://proxy.my.domain:3249'
-
[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:
-
defaults
(Object)
Properties
-
[useHTTPS=false]
(Boolean)
-
[useCompression=true]
(Boolean)
 Send 'Accept-encoding: gzip' header to server & unzip zipper responses
-
[keepAlive=false]
(Boolean)
 Use keep Alive HTTP protocol feature if server support it.
-
[sendTimeout=30000]
(Number)
 Send timeout in ms.
-
[receiveTimeout=30000]
(Number)
 Receive timeout in ms.
-
[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:
-
options
(Object|String)
 Either URL string in format protocol://host:port/path
or config
Properties
-
[URL]
(String)
 Service URL in format protocol://host:port/path
. Will override useHTTPS
, server
, host
, port
and path
if passed
-
[server]
(String)
 DEPRECATED. Server to connect in format 'host:port' or 'host' in case port == 80.
-
[host]
(String)
 Host to connect. If server
not specified this value used
-
[port]
(String)
 Port. Default is 80 for HTTP or 443 for HTTPS
-
[path='/']
(String)
 Request path. Defaults to '/'. Should include query string if any. E.G. '/index.html?page=12'
-
[method='GET']
(String)
 HTTP method to use for request
-
[headers]
(Object.<string, string>)
 An object containing request headers
-
[useHTTPS=false]
(Boolean)
-
[useCompression=true]
(Boolean)
 Send 'Accept-encoding: gzip' header to server & unzip zipper responses
-
[keepAlive=false]
(Boolean)
 Use keep Alive HTTP protocol feature if server support it.
-
[sendTimeout=30000]
(Number)
 Send timeout in ms.
-
[receiveTimeout=30000]
(Number)
 Receive timeout in ms.
-
[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:
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:
-
options
(Object)
 Request options as described in http.request
-
[URLParams]
(Object)
 optional parameters to add to options.path
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:
-
proxy
(String)
 name of the proxy server to use in format[[http|https]://]host[:port]
For example 'http://proxy.my.domain:3249' -
[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:
-
defaults
(Object)
Properties
-
[useHTTPS=false]
(Boolean)
-
[useCompression=true]
(Boolean)
 Send 'Accept-encoding: gzip' header to server & unzip zipper responses -
[keepAlive=false]
(Boolean)
 Use keep Alive HTTP protocol feature if server support it. -
[sendTimeout=30000]
(Number)
 Send timeout in ms. -
[receiveTimeout=30000]
(Number)
 Receive timeout in ms. -
[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:
-
options
(Object|String)
 Either URL string in formatprotocol://host:port/path
or configProperties
-
[URL]
(String)
 Service URL in formatprotocol://host:port/path
. Will overrideuseHTTPS
,server
,host
,port
andpath
if passed -
[server]
(String)
 DEPRECATED. Server to connect in format 'host:port' or 'host' in case port == 80. -
[host]
(String)
 Host to connect. Ifserver
not specified this value used -
[port]
(String)
 Port. Default is 80 for HTTP or 443 for HTTPS -
[path='/']
(String)
 Request path. Defaults to '/'. Should include query string if any. E.G. '/index.html?page=12' -
[method='GET']
(String)
 HTTP method to use for request -
[headers]
(Object.<string, string>)
 An object containing request headers -
[useHTTPS=false]
(Boolean)
-
[useCompression=true]
(Boolean)
 Send 'Accept-encoding: gzip' header to server & unzip zipper responses -
[keepAlive=false]
(Boolean)
 Use keep Alive HTTP protocol feature if server support it. -
[sendTimeout=30000]
(Number)
 Send timeout in ms. -
[receiveTimeout=30000]
(Number)
 Receive timeout in ms. -
[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:
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:
-
options
(Object)
 Request options as described inhttp.request
-
[URLParams]
(Object)
 optional parameters to add to options.path