parallel-http-request
Simplicity to create multiple HTTP Request in Parallel for NodeJS.
Install using NPM
$ npm install parallel-http-request
Usage
Set Config
var ParallelRequest = ; var config = response: "simple" // [optional] detail|simple|unirest, if empty then the response output is simple; var request = config; //or without configvar request = ;
The config.response
options value is :
simple
: minimalist output response.detail
: output response very detail.unirest
: output response with unirest format.
Make Request
request ; // orrequest ; // or mixed itrequest ;
Documentation
This Parallel HTTP Request is created based on unirest-nodejs library, so request.options is almost similar with unirest
.
Note:
- This library is intended to create multiple request in parallel, so not all
unirest
feature is worked. Please see Limitation.
request.add(options)
To make a multiple http request, you have to use .add() for each request.
request.add(string|object)
this will add your request into collection.
request.add(string)
this request will use default methodGET
.request.add(object)
this request will useobject options
.
request.send(callback)
This will execute your multiple request.
Return output is always array object
.
request;
Options object in request.add(options)
url: (String)
- Url to send the request.method: (String)
- DefaultGET
; HTTP Method.headers: (Object)
- Default{}
; HTTP Headers.query: (Object)
- HTTP URI Parameter.body: (String | Object)
- Entity body for certain requests.form: (Object)
- Form Data.field: (Object)
- Form fields.attach: (Object)
- For handle files.followRedirect: (Boolean)
- Defaulttrue
; Follow HTTP3xx
responses as redirects.followAllRedirects: (Boolean)
- Defaultfalse
; Follow Non-GET HTTP3xx
responses as redirects.maxRedirects: (Number)
- Default10
; Maximum number of redirects before aborting.timeout: (Number)
- Number of milliseconds to wait before aborting.encoding: (String)
- Encoding to be used onsetEncoding
of response data.strictSSL: (Boolean)
- Defaulttrue
; SetsstrictSSL
flag to require that SSL certificates be valid.httpSignature: (Object)
- HTTP Signature.proxy: (String)
- HTTP Proxy.secureProtocol: (Object)
- Sets the secure protocol to use.localAddress: (Object)
- SetslocalAddress
, local interface to bind for network connections.auth: (Object)
- Accepts either an Object containinguser
,pass
, and optionallysendImmediately
.aws: (Object)
- Setsaws
, AWS Signing Credentials.hawk: (Object)
- Setshawk
, HAWK Signing Credentials.cookie: (String)
- Creates a cookie.
request.remove(name)
This will remove url in collection.
name
is the url of the request.
request;
request.clean()
This will cleanup all request in collection.
request;
request.getCollection()
This will return all request in collection.
request;
request.unirest
If you want to use unirest
(the underlying layer of parallel-http-request) directly.
Because Sometimes we are not always have to call request in parallel.
Please see Unirest Documentation.
requestunirest ;
Example
Request with Method
You can just set method request by like this
request;request;request;request;request;request;request;
Request with Query / Body / Form
POST with Query Parameter
request;
POST with Body
request;
POST with Form Data Encoded
request ;
POST with Body HTML / Other
request;
Request with Upload File
request;
Request with timeout
request;
Request with encoding
request;
Request with followRedirect
request;
Request with maxRedirects
request;
Request with strictSSL
request;
Request with proxy
request;
Request with secureProtocol
request;
Request with localAddress
request;
Request with auth
request;
Request with aws
request;
Request with hawk
request;
Request with cookie
Create request with cookie
. Please see documentation.
request;
Request with Jar
Creates a container to store multiple cookies, i.e. a cookie jar.
var cookieJar = requestjar;cookieJar;// or as argumentcookieJar; request;
Limitation
There is several feature which is not posible to do with multiple parallel request.
oauth
- Sets oauth, list of oauth credentials.part
- StillExperimental
; Similiar to request multipart.then
- promise function.pool
- Single request; for socket connection which is use for single connection.forever
- Keeps socket connections alive between keep-alive in requests.
The solution about this limitation is you have to directly use unirest
libary.
Example to use Unirest directly
Request with oAuth
requestunirest ;
Please see Unirest Documentation.
Unit Test
$ npm test