Simplicity to create multiple HTTP Request in Parallel for NodeJS.
$ npm install parallel-http-request
var ParallelRequest = require('parallel-http-request');
var config = {
response: "simple" // [optional] detail|simple|unirest, if empty then the response output is simple
};
var request = new ParallelRequest(config);
//or without config
var request = new ParallelRequest();
The config.response
options value is :
simple
: minimalist output response.detail
: output response very detail.unirest
: output response with unirest format.
request.add('https://jsonplaceholder.typicode.com/posts/1')
.add('https://jsonplaceholder.typicode.com/posts/2')
.send(function(response){
console.log(response);
});
// or
request.add({url:'https://jsonplaceholder.typicode.com/posts/1',method:'get'})
.add({url:'https://jsonplaceholder.typicode.com/posts/2',method:'get'})
.send(function(response){
console.log(response);
});
// or mixed it
request.add({
url:'https://jsonplaceholder.typicode.com/posts/1',
method:'get'
})
.add({
url:'https://jsonplaceholder.typicode.com/posts/2',
method:'post',
headers:{'Content-Type':'application/json'},
body: {
fullname:'value',
address:'value'
}
})
.add({
url:'https://jsonplaceholder.typicode.com/posts/3',
method:'delete',
headers:{'Content-Type':'application/json'},
body: {
fullname:'value',
address:'value'
}
})
.send(function(response){
console.log(response);
});
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.
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
.
This will execute your multiple request.
Return output is always array object
.
request.send(function(response){
console.log(response)
});
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.
This will remove url in collection.
name
is the url of the request.
request.remove('https://google.com');
This will cleanup all request in collection.
request.clean();
This will return all request in collection.
request.getCollection();
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.
request.unirest.get('https://google.com')
.end(function(response){
console.log(response.body)
});
You can just set method request by like this
request.add({url:'https://www.google.com', method:'get'});
request.add({url:'https://www.google.com', method:'post'});
request.add({url:'https://www.google.com', method:'put'});
request.add({url:'https://www.google.com', method:'patch'});
request.add({url:'https://www.google.com', method:'delete'});
request.add({url:'https://www.google.com', method:'head'});
request.add({url:'https://www.google.com', method:'options'});
request.add({
url:'https://jsonplaceholder.typicode.com/posts/1',
method:'post',
query: {
search:'value'
}
});
request.add({
url:'https://jsonplaceholder.typicode.com/posts/1',
method:'post',
headers:{'Content-Type':'application/json'}
body: {
fullname:'value',
address:'value'
}
});
request.add({
url:'https://jsonplaceholder.typicode.com/posts/1',
method:'post',
headers:{'Content-Type':'application/x-www-form-urlencoded'}
form: {
fullname:'value',
address:'value'
}
})
.add({
url:'https://jsonplaceholder.typicode.com/posts/2',
method:'post',
headers:{'Content-Type':'application/x-www-form-urlencoded'}
body: JSON.stringify({
fullname:'value',
address:'value'
})
})
.add({
url:'https://jsonplaceholder.typicode.com/posts/3',
method:'post',
headers:{'Content-Type':'application/x-www-form-urlencoded'}
body: 'name=nijiko&pet=turtle'
});
request.add({
url:'https://jsonplaceholder.typicode.com/posts/1',
method:'post',
headers:{'Content-Type':'text/html'}
body: '<strong>Hello World!</strong>'
});
request.add({
url:'https://mockbin.com/request',
method:'post',
headers:{
'Content-Type': 'multipart/form-data',
'Content-Length': fs.statSync(path.resolve('favicon.ico')).size
},
attach:{
'file':fs.createReadStream(path.resolve('favicon.ico')),
'remote file':request.unirest.request('https://google.com/favicon.ico')
}
});
request.add({
url:'https://www.google.com',
timeout:60000
});
request.add({
url:'https://www.google.com',
encoding:'utf-8'
});
request.add({
url:'https://www.google.com',
followRedirect:true
});
request.add({
url:'https://www.google.com',
maxRedirects:5
});
request.add({
url:'https://www.google.com',
strictSSL:false
});
request.add({
url:'https://www.google.com',
proxy:'https://localproxy.com'
});
request.add({
url:'https://www.google.com',
secureProtocol:'SSLv3_client_method'
});
request.add({
url:'https://www.google.com',
localAddress:'127.0.0.1'
});
request.add({
url:'https://www.google.com',
auth:{
user: 'Nijiko',
pass: 'insecure',
sendImmediately: true
}
});
request.add({
url:'https://www.google.com',
aws:{
key: 'AWS_S3_KEY',
secret: 'AWS_S3_SECRET',
bucket: 'BUCKET NAME'
}
});
request.add({
url:'https://www.google.com',
hawk:{
credentials: {
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
algorithm: 'sha256',
user: 'Steve'
}
}
});
Create request with cookie
. Please see documentation.
request.add({
url:'https://www.google.com',
cookie:'yummy_cookie=choco; tasty_cookie=strawberry'
});
Creates a container to store multiple cookies, i.e. a cookie jar.
var cookieJar = request.jar;
cookieJar.add(request.cookie('yummy_cookie=choco; tasty_cookie=strawberry'));
// or as argument
cookieJar.add('key=value', '/');
request.add({
url:'https://google.com',
jar:cookieJar
});
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.
request.unirest
.get('https://api.twitter.com/oauth/request_token')
.oauth({
callback: 'https://mysite.com/callback/',
consumer_key: 'CONSUMER_KEY',
consumer_secret: 'CONSUMER_SECRET'
})
.then(response => {
let access_token = response.body
return request.unirest
.post('https://api.twitter.com/oauth/access_token')
.oauth({
consumer_key: 'CONSUMER_KEY',
consumer_secret: 'CONSUMER_SECRET',
token: access_token.oauth_token,
verifier: token: access_token.oauth_verifier
})
})
.then((response) => {
var token = response.body
return request.unirest
.get('https://api.twitter.com/1/users/show.json')
.oauth({
consumer_key: 'CONSUMER_KEY',
consumer_secret: 'CONSUMER_SECRET',
token: token.oauth_token,
token_secret: token.oauth_token_secret
})
.query({
screen_name: token.screen_name,
user_id: token.user_id
})
})
.then((response) => {
console.log(response.body)
});
Please see Unirest Documentation.
$ npm test