Skip to content

XML HTTP Requester for Node.js special for React and React Native applications

Notifications You must be signed in to change notification settings

pavex/js-httpclient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

XML HTTP Requester for Node.js special for React and React Native applications

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published