0.1.8 • Published 9 years ago
http-api-client v0.1.8
http-api-client
An easy to use NodeJS HTTP API client library.
Installation
npm install http-api-client --save
Usage
var client = require('http-api-client');
client.request({
url: 'http://hawttrends.appspot.com/api/terms/'
}).then(function (response) {
response.getStatusCode(); // returns the HTTP status code
console.log(response.getData()); // returns the string representation of the response body
response.getBuffer(); // returns the raw response Buffer object http://nodejs.org/api/buffer.html
response.getNativeResonse(); // returns the native NodeJS repsonse object
response.getJSON(); // returns a JSON-parsed repsonse
});
API
client.request({
url: 'http://example.com', // required, protocol can be HTTPS
method: 'POST', // optional
encoding: 'utf8', // optional
parameters: { param: 'value' } // optional POST parameters,
cookies: { // optional object containing cookies
cookieName: cookieValue
},
data: rawRequestBody // optional raw request body data
files: { // optional file uploads
fieldname: {
name: 'filename',
type: 'application/octet-stream',
data: fileDataBuffer
}
}
}).then(function (/* Response object */response) {
// Success
response.request({
url: 'http://example.com/something'
}); // will make a request perserving the cookies from the previous request
}, function (error) {
// Failure
});