Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

@natlibfi/http-client-x

1.0.3 • Public • Published

HTTP client X NPM Version Build Status Test Coverage

Simple HTTP client that works both in Node.js and browser.

Usage

HTTP client X provides convenience methods for common HTTP requests. Each method returns a Promise which is rejected if HTTP status is an error (Status code is greater or equal to 400). The same result object is passed to both resolve and reject callbacks (Unless the request fails and no HTTP response data is available).

  • get, head, delete: These methods take a single argument. Either a string representing the URL the request is send to or an object with properties url and optional headers
  • post, put: These methods either takes two string arguments url and body or an object with properties url and optional body and headers
  • request: This method either takes two string arguments method and url or an object with properties method, url, and optional body and headers

Behind the scenes, HTTP client X uses XMLHttpRequest in browsers and http/https modules in Node.js. Therefore testing software using HTTP client X can be done by faking those implementations or using http-client-x-mock which provides an unified API for mocking.

Node.js

var http_client = require('http-client-x/lib/nodejs/main');
 
http_client.get('http://foo.bar').then(function(response) {
 
  console.log(response.status);
  console.log(response.body);
  console.log(response.headers);
 
}).catch(function(e) {
  console.error(e);      
});
 

AMD

define(['http-client-x/lib/browser/main'], function(http_client) {
 
  http_client.get('http://foo.bar').then(function(response) {
 
    console.log(response.status);
    console.log(response.body);
    console.log(response.headers);
 
  }).catch(function(e) {
    console.error(e);      
  });
 
});

Development

Clone the sources and install the package using npm:

npm install

Run the following NPM script to lint, test and check coverage of the code:

 
npm run check
 

License and copyright

Copyright (c) 2016-2017 University Of Helsinki (The National Library Of Finland)

This project's source code is licensed under the terms of GNU Lesser General Public License Version 3 or any later version.

Install

npm i @natlibfi/http-client-x

DownloadsWeekly Downloads

1

Version

1.0.3

License

LGPL-3.0+

Last publish

Collaborators