@yottabrain/thin-http-client

1.0.0 • Public • Published

Build Status version Coverage Status

thin-http-client

  • It is wrapper around the default node libraries http and https
  • As name says it is a thin and it does not have any dependencies
  • Can be used with async/await or promise

Install

npm install @yottabrain/thin-http-client

Usage

Include HttpClient

const { HttpClient } = require('@yottabrain/thin-http-client');

Invoke GET method

const ENDPOINT = "https://example.com/api/someapi";

let res = await new HttpClient().get(ENDPOINT);

Invoke Post method

let body = {"key": "value"};
let res = await new HttpClient().post(ENDPOINT, body);

Response

# Check http status code is 200
console.log(res.success());

# Raw response body
console.log(res.raw());

# Response body as JSON
console.log(res.json());

#  Original response object provided by Node http/https module
console.log(res.response());

Call API with Encoding and headers

let res = await new HttpClient().setEncoding('UTF-8').setHeaders({ "Authorization": token }).get(ENDPOINT);

Set HTTP options

You can set the node module http/https options in constructor. Refer this link for the list of options https://nodejs.org/api/http.html#http_http_request_options_callback

let options = {
    auth: 'user1:pwd123'
}
let res = await new HttpClient(options).get(ENDPOINT);

Using promise instead of await

new HttpClient().get(ENDPOINT).then(res => {
    
});

Enable debug logs

await new HttpClient().debug(true).get(ENDPOINT);

Package Sidebar

Install

npm i @yottabrain/thin-http-client

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

10.8 kB

Total Files

8

Last publish

Collaborators

  • esiva