README
http-request-plus
Small wrapper around {http,https}.request()
Features:
- HTTP & HTTPs
- promise oriented
- stream oriented
- cancelable via cancel token or
response.cancel()
- request body can be either a buffer/string or a stream
- content length header automatically set if available
- support
pathname
&query
(string or object) if nopath
provided - handle redirects
- response emits
error
on abort and timeout URL
objects can be used as params
Install
Installation of the npm package:
> npm install --save http-request-plus
Usage
Example
Easiest use case: just downloads and prints a page with error handling.
ES2015 - ES2016:
import httpRequestPlus from "http-request-plus";
(async () => {
try {
console.log(
await httpRequestPlus("http://example.org", {
// A request body can provided, either as a buffer/string or a stream
body: "foo bar",
// By default, http-request-plus throws if the reponse's status Code is not 2xx
//
// This option can be used to bypass this
bypassStatusCheck: true,
// Maximum number of redirects that should be handled by http-request-plus
//
// Defaults to 5
maxRedirects: 0,
onRequest(request) {
// this function will be called multiple times in case of redirections
request.setTimeout(10 * 1e3);
request.on("timeout", request.abort);
},
// all other options are forwarded to native {http,https}.request()
}).readAll("utf8")
);
} catch (error) {
console.error("An error as occured", error);
}
})();
ES5:
var httpRequestPlus = require("http-request-plus");
httpRequestPlus("http://example.org")
.readAll("utf8")
.then((body) => {
console.log(body);
})
.catch((error) => {
console.error("An error as occured", error);
});
HTTP method helpers
httpRequestPlus.delete();
httpRequestPlus.head();
httpRequestPlus.patch();
httpRequestPlus.post();
httpRequestPlus.put();
httpRequestPlus.extend(opts)
const githubRequest = httpRequestPlus.extend("https://github.com");
githubRequest.post("/api");
→ httpRequestPlus(options...)Promise<response>
Promise<response>.cancel()
→ Promise<response>.readAll()Promise<buffer>
response.cancel()
→ response.readAll()Promise<buffer>
response.length
error.code
error.response
Development
# Install dependencies
> npm install
# Run the tests
> npm test
# Continuously compile
> npm run dev
# Continuously run the tests
> npm run dev-test
# Build for production (automatically called by npm install)
> npm run build
Contributions
Contributions are very welcomed, either on the documentation or on the code.
You may:
- report any issue you've encountered;
- fork and create a pull request.
License
ISC © Julien Fontanet