js-httpclient

0.0.3 • Public • Published

js-httpclient

Simple XML HTTP Requester for Node.js special for React and React Native applications based on XMLHttpRequest

No long explanation, here are examples:

import {HttpClient} from 'js-httpclient';
 
var client = new HttpClient();
var r = new HttpClient.Request();
 
r.setUrl('https://raw.githubusercontent.com/pavex/js-httpclient/master/samples/sample.json');
r.setContentType('application/json');
r.setMethod('GET');
r.setParams({});
 
client.send(r)
    .then((response) => {
        console.log(response.getStatus());
        console.log(response.getContentType());
        console.log(response.getHeaders());
        console.log(response.getBody());
    })
    .catch((error) => {
        console.log(error);
    });

Using in React Native

import {JsonRequest} from 'publixe-httpclient';
 
// Customize xhr for Chrome debugging
var xhr = GLOBAL.originalXMLHttpRequest ? GLOBAL.originalXMLHttpRequest : XMLHttpRequest;
var r = new JsonRequest(xhr);
 
r.setUrl('https://raw.githubusercontent.com/pavex/js-httpclient/master/samples/sample.json');
r.setContentType('application/json');
r.setMethod('GET');
r.setParams({});
 
// Add content type in classic way
if (xhr == XMLHttpRequest) {
    r.setHeader('content-type', 'application/json');
}
 
r.onSuccess(() => {
    console.log(r.getStatus());                // Status code
    console.log(r.getResponse());              // Completely response object
    console.log(r.getData());                  // Only contents of root element "data"
});
r.onFailure(() => {
    console.log(r.getStatus());                // Status code
    console.log(r.getLastError());             // Error message generated by requester
    console.log(r.getErrorObject());           // Error object included message and code
});
 
r.send();

I try to prepare short documentation of this class.

Package Sidebar

Install

npm i js-httpclient

Weekly Downloads

5

Version

0.0.3

License

MIT

Unpacked Size

16 kB

Total Files

14

Last publish

Collaborators

  • pavex