publixe-httpclient
v0.0.4
Published
HTTP JSON client for React and React Native application
Downloads
4
Maintainers
Readme
publixe-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 'publixe-httpclient';
var client = new HttpClient();
var r = new HttpClient.Request();
r.setUrl('https://raw.githubusercontent.com/pavex/publixe-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/publixe-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.