ES6 Promise-base http request agent
Install
npm install request-agent --save
Init httpAgent
const requestAgent = require('request-agent');
Configure your http-request entity
0. An example
requestAgent.headers({'content-type':'application/json'})
.headers({ 'user-agent':'customer UA' , 'power-by':'lanhao@fyscu' })
.query({ 'page':1 , 'pageSize':10 })
.query({ '_t':1465317270 })
.body({ 'title':'I say' , 'content':'Nothing' })
.method('post')
.send()
.then(requestAgent.toJson) // this step is optional
.then( (obj) => {
//do something
console.log(obj);
})
.catch( (err) => {
//error handler
console.trace(err);
});
1.http headers
requestAgent.headers({'content-type':'application/json'} [ ,flush=false ])
setting multi-headers in 2 ways:
requestAgent.headers({'content-type':'application/json','UA':'MyAgent'})
or
requestAgent.headers({'content-type':'application/json'}).headers({'UA':'MyAgent'})
Parameter flush
, default false ,told the request-agent clear the old headers or not.
2.http query string
requestAgent.query({'_t':1441089765} [ ,flush=false ])
some like headers
.
3.http body
requestAgent.body({'content':'I send this message to server'} [ ,flush=false ])
some like headers
.