JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 63904
  • Score
    100M100P100Q152878F
  • License BSD-3-Clause

An opinionated, isomorphic HTTP client.

Package Exports

  • @digitalbazaar/http-client

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@digitalbazaar/http-client) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

http-client

An opinionated, isomorphic HTTP client.

Usage

Import httpClient

import {httpClient} from '@digitalbazaar/http-client';

GET a JSON response in the browser

try {
  result = await httpClient.get('http://httpbin.org/json');
  return result.data;
} catch(e) {
  // status is HTTP status code
  // data is JSON error from the server
  const {data, status} = e;
  throw e;
}

GET a JSON response in Node with a HTTP Agent

import https from 'https';
// use an agent to avoid self-signed certificate errors
const agent = new https.agent({rejectUnauthorized: false});
try {
  result = await httpClient.get('http://httpbin.org/json', {agent});
  return result.data;
} catch(e) {
  // status is HTTP status code
  // data is JSON error from the server if available
  const {data, status} = e;
  throw e;
}

GET HTML by overriding default headers

const headers = {Accept: 'text/html'};
try {
  result = await httpClient.get('http://httpbin.org/json', {headers});
  // see: https://developer.mozilla.org/en-US/docs/Web/API/Body#Methods
  return result.response.text();
} catch(e) {
  // status is HTTP status code
  // any message from the server can be parsed from the response if present
  const {response, status} = e;
  throw e;
}

POST a JSON payload

try {
  result = await httpClient.post('http://httpbin.org/json', {
    json: {some: 'data'}
  });
  return result.data;
} catch(e) {
  // status is HTTP status code
  // data is JSON error from the server
  const {data, status} = e;
  throw e;
}