thip
v0.2.0
Published
Ultra-Thin, Dependency-Free, Promise-Based HTTP request client.
Downloads
1
Readme
thip
Ultra-Thin, Dependency-Free, Promise-Based HTTP request client.
Features
- Ultra thin, and lightweight
- Dependency free
- Promise based
- Only supports Node.js (>= 6), not supports browser
- Supports HTTPS
- Follows redirect by default
Install
npm install thip
Usage
thip(options[, data])
options
<object>|<string> - Any options not included below will be passed to http.request(options).url
<string> - If you specified this option, parse the url and merged to other options.followRedirect
<boolean> - Follow HTTP 3xx responses as redirects. Defaults totrue
.maxRedirects
<number> - The maximum number of redirects to follow. Defaults to10
.
- data <string>|<buffer> - This will be passed to request.write(chunk).
This function returns Promise
object which resolves response object.
The response object is an instance of http.IncomingMessage, but contains body
property.
const thip = require('thip');
thip('http://example.com/').then((res) => {
console.log(res.body);
});
const thip = require('thip');
const querystring = require('querystring');
const data = querystring.stringify({
'msg': 'Hello World!',
});
const options = {
method: 'POST',
url: 'http://example.com/message',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data),
},
};
thip(options, data).then((res) => {
console.log('Post successful!');
});
thip.get(options[, data])
options.method
assigns"GET"
by default.- If
data
is <object>, the data is encoded as URL query.
thip.get('http://example.com/', { foo: 'bar' });
This is same as below.
thip('http://example.com/?foo=bar');
thip.post(options[, data])
options.method
assigns"POST"
by default.- If
data
is <object>, the data is encoded asx-www-form-urlencoded
.
const thip = require('thip');
thip.post('http://example.com/', { foo: 'bar' });
This is same as below.
const thip = require('thip');
const querystring = require('querystring');
const data = querystring.stringify({
'foo': 'bar',
});
const options = {
method: 'POST',
url: 'http://example.com/',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data),
},
};
thip(options, data);
Error Handling
If HTTP response status code is 4xx or 5xx, the promise is rejected with HttpClientError or HttpServerError.
thip('http://example.com/not_found_path').catch((error, res) => {
console.log(error.name); // "HttpClientError"
console.log(res.statusCode); // 404
})