A function that expects the common options object outlined in this specification
A function that initiates the actual HTTP request while consuming the options outlined in this specification
module.exports = (options) => { // do something with options // and make the actual HTTP request}
Given the above module definition, a client application can use it like this:
var request = require('my-http-client')// make requestrequest({ // any common option defined in this specification})
HTTP Client Wrappers
var http
=
require
(
'http'
)
module.exports = (options) => {
// do something with the common interface options
var resultOptions = {}
// implement various HTTP features
return http.request(resultOptions)
}
var request = require('request')module.exports = (options) => { // do something with the common interface options var resultOptions = {} // implement various HTTP features return request(resultOptions)}
// use the native fetch API in the browsermodule.exports = (options) => { // do something with the common interface options var resultOptions = {} // implement various HTTP features return fetch(new Request(url, resultOptions))}
Either way the client application should be able to make requests in a consistent way:
var request = require('my-http-client')// make requestrequest({ // any common option defined in this specification})
Optional Dependencies
A module conforming to this specification while having optional dependencies may look like this:
module.exports = (deps) => (options) => { var resultOptions = {} if (options.oauth) { resultOptions.oauth = deps.oauth(options.oauth) } return request(resultOptions)}
Given the above module definition, a client application can use it like this:
var request = require('my-http-client')({ oauth: require('my-oauth-implementation')})// make requestrequest({ // any common option defined in this specification})
Bundled Dependencies
A module conforming to this specification while having hardcoded dependencies may look like this:
new require('tough-cookie).CookieJar(store, options)
length Boolean
true defaults to false if omitted
callback Function
function (err, res, body) {} buffers the response body
by default the response buffer is decoded into string using utf8.
Set the encoding property to binary if you expect binary data, or any other specific encoding.
redirect Boolean | Object
true
follow redirects for GET, HEAD, OPTIONS and TRACE requests
Object
all - follow all redirects
max - maximum redirects allowed
removeReferer - remove the referer header on redirect
allow - function (res) user defined function to check if the redirect should be allowed
timeout Number
integer indicating the number of milliseconds to wait for a server to send response headers (and start the response body) before aborting the request. Note that if the underlying TCP connection cannot be established, the OS-wide TCP connection timeout will overrule the timeout option
har Object
end Boolean
true
tries to automatically end the request on nextTick