Ultra-Thin, Dependency-Free, Promise-Based HTTP request client.
- Ultra thin, and lightweight
- Dependency free
- Promise based
- Only supports Node.js (>= 6), not supports browser
- Supports HTTPS
- Follows redirect by default
npm install thip
-
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!');
});
-
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');
-
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);