hahoorequest
v0.7.0
Published
A HTTP request abstract library, use other http request libraries in one way. Isomorphic request to work in Node or in the browser.
Downloads
62
Maintainers
Readme
hahoorequest
A HTTP request abstract library, use other http request libraries in one way. Isomorphic request to work in Node or in the browser. For example React isomorphism, the same code for the server and the browser.
Why
- Different libraries use different syntax, the replacement library will lead to a large number of changes
- The browser and the server can use the same code for different libraries. For example: the browser with reqwest, the server with request. Or both the browser and the server are using fetch.
Install
npm install hahoorequest --save
Import library
Select the library you want to use
Browser & Server
Browser with fetch, Server with fetch
import request from 'hahoorequest/lib/fetch';
Browser with fetch, Server with request
import request from 'hahoorequest/lib/fetch-request';
Browser with reqwest, Server with fetch
import request from 'hahoorequest/lib/reqwest-fetch';
Browser with reqwest, Server with request
import request from 'hahoorequest/lib/reqwest-request';
Browser with superagent, Server with superagent
import request from 'hahoorequest/lib/superagent';
Browser with superagent, Server with fetch
import request from 'hahoorequest/lib/superagent-fetch';
Browser with superagent, Server with request
import request from 'hahoorequest/lib/superagent-request';
Browser
fetch
import request from 'hahoorequest/lib/whatwg-fetch';
reqwest
import request from 'hahoorequest/lib/reqwest';
superagent
import request from 'hahoorequest/lib/superagent';
Server
node-fetch
import request from 'hahoorequest/lib/node-fetch';
request
import request from 'hahoorequest/lib/node-request';
Usage
Return Promises
request({
url: 'https://raw.githubusercontent.com/hahoocn/hahoorequest/master/test/test.json',
method: 'GET',
type: 'json'
})
.then((res) => {
console.log(res.body);
});
Options
url
a fully qualified urimethod
http method (default:GET
)headers
http headersbody
entity body forPATCH
,POST
andPUT
requests. Must be a queryString
orJSON
objecttype
a string enum.html
,xml
,json
,form
,png
... (default:json
)qs
object containing querystring values to be appended to the urlcredentials
Sending cookies. fetch set credentials option. if credentials is not undefined, reqwest and superagent will addwithCredentials = true
Response
response.body
Content of responseresponse.status
http status code
Errors
response.errcode
Code of errorsresponse.errmsg
Content of errors