kusarequest
Promisified simple request module. This module focuses on chainable request.
This module is wrapped to request and bluebird.
Installation
var Kusarequest = require ( " kusarequest " ) ;
new Kusarequest . get ( ... )
Usage
Basic request
Kusarequest will use in the instance. And instance will request in the HTTP method.
Kusarequest support HTTP method => ["get", "head", "post", "put", "patch", "del"]
It return Promise. Promise's Argument is Kusarequest instance.
new Kusarequest ( ) . get ( " https://example.com " ) . then ( function ( kusarequest ) {
assert . strictEqual ( kusarequest . res . body , " get " ) ;
} ) ;
Request with option
Option is equivalent to the request module option.
For more information see here
new Kusarequest ( ) . get ( " https://example.com/option " , {
" qs " : {
" key " : " get_value "
}
} )
. then ( function ( kusarequest ) {
assert . strictEqual ( kusarequest . res . body , " get_value " ) ;
} ) ;
Request with callback
Promise to return from the callback is executed.
Callback of this it has been bound from the instance.
new Kusarequest ( ) . get ( " https://example.com " , function ( res ) {
this . container = res . body + " container " ;
} )
. then ( function ( kusarequest ) {
assert . strictEqual ( kusarequest . container , " getcontainer " ) ;
} ) ;
Request with option and callback
Kusarequest ( ) . get ( " https://example.com/option " , {
" qs " : {
key : " get_value "
}
} , function ( res ) {
this . container = res . body + " container " ;
} )
. then ( function ( kusarequest ) {
assert . strictEqual ( kusarequest . container , " get_valuecontainer " ) ;
} ) ;
Feature
Chaninable
Kusarequest has a method chain concept.
Kusarequest ( ) . post ( " https://example.com/form " , {
" form " : {
" key " : " post_value "
}
} )
. then ( function ( kusarequest ) {
assert . strictEqual ( kusarequest . res . body , " post post_value " ) ;
} ) ;
path only ok
Uri of the instance is okay only path.
Kusarequest ( ) . get ( " https://example.com/ " )
. then ( function ( kusarequest ) {
return kusarequest . get ( " /some/path " ) ;
} )
. then ( function ( kusarequest ) {
assert . strictEqual ( kusarequest . res . body , " path " ) ;
} ) ;
Continuation of cookie
Kusarequest instance will take over the cookie of the each request.
This is useful, for example, when you are scraping after login.
kusarequest . post ( " https://example.com/login " , {
" form " : {
" id " : " id " ,
" password " : " password "
}
} )
. then ( function ( kusareq ) {
kusareq . get ( ... )
} )
You can also set the cookie-jar when you make the instance.
var jar ;
new Kusarequest ( ) . post ( " https://example.com/login " )
. then ( function ( kusarequest ) {
jar = kusarequest . jar ( ) ;
} )
new Kusarequest ( jar ) . get ( " /after/login/pass " ) ;
. then ( function ( kusarequest ) {
assert . strictEqual ( kusarequest . res . body , " success " ) ;
} ) ;
Follow all redirect
Redirection of GET and non-GET HTTP method is automatically follow.
kusarequest . get ( " /redirect-from " )
. then ( function ( kusareq ) {
console . log ( kusareq . url )
} ) ;
kusarequest . post ( " /redirect-from " )
. then ( function ( kusareq ) {
console . log ( kusareq . url )
} ) ;
Response history
Kusarequest instance has a history of each response to r_histories
property.
Kusarequest ( ) . get ( BASE_URL )
. then ( function ( kusareqest ) {
return kusareqest . get ( " /path " ) ;
} )
. then ( function ( kusareqest ) {
return kusareqest . get ( " /option " , { " qs " : { " key " : " option " } } ) ;
} )
. then ( function ( kusareqest ) {
var bodys = kusareqest . r_histories . map ( function ( res ) {
return res . body ;
} ) ;
assert . strictEqual ( bodys . join ( " " ) , " option path get " ) ;
} ) ;
Instance property and method
instance propaty is this.
property
res
- request's response.toJSON()
raw_res
- original request response
r_history
- res
history
container
- Use this when you want to exceed the each [then]
method