A simple promise wrapper around Node.JS http(s) ClientRequest
shh-node-http is designed to be the simplest way to make http calls from your Node application. For convenience it defaults to a Content-Type of JSON and follows redirects.
It supports:
- http
- https
- parsing JSON
- parsing form
- passthrough of all other body type
shh-node-http has 0 (zero) dependencies, is under 250 LOC including comments / whitespace, and completely exposes the native Node request / response objects so nothing is hidden from you.
shh-node-http has full typings and works with TypeScript, esmodule import
, and the old Node module require
.
const { shh } = require('shh-node-http');
shh.post(url: string, body: any, options?: Object)
.then(response => doSomething(response))
.catch(e => handleError(e));
- params -
{ key: value }
query parameters (defaultnull
) - headers -
{ key: value }
http headers (automatically appliesContent-Type, Content-Length
) - json -
true|false
encodes and parses the request body as JSON (defaulttrue
) - form -
true|false
form encodes and parses the request body (defaultfalse
) - timeout -
number
request timeout in milliseconds, the request will be canceled and the Promise will be rejected once this value is reached (default30000
- 30 seconds) - follow_redirects -
true|false
whether to follow http redirects (defaulttrue
)
const { shh } = require('shh-node-http');
shh
.get('https://www.google.com/', { json: false })
.then(response => {
console.log('status: ', response.statusCode);
console.log('message: ', response.statusMessage);
})
.catch(e => console.error('request failed with error: ', e));
const { shh } = require('shh-node-http');
shh
.get('https://my-cool-rest-api.com/api/v1/users', { params: { name: 'Bob' } })
.then(response => {
console.log('status: ', response.statusCode);
console.log('message: ', response.statusMessage);
})
.catch(e => console.error('request failed with error: ', e));
const { shh } = require('shh-node-http');
shh
.post('https://my-cool-rest-api.com/api/v1/users', {
name: 'Josh',
email: '[email protected]',
password: 'hunter2'
})
.then(response => {
console.log('Created user ', response.body.name);
hanldeNewUser(response.body);
})
.catch(e => {
console.error('Create user failed woth error: ', e.message);
handleNewUserError(e.body || e);
});
const { shh, utils } = require('shh-node-http');
shh
.request('GET', 'https://www.google.com/', null, { json: false })
.then(response => utils.assertStatus(response))
.then(response => {
console.log('status: ', response.statusCode);
console.log('message: ', response.statusMessage);
})
.catch(e => console.error('request failed with error: ', e));