Description
Requestify is a super easy to use and extendable HTTP client for nodeJS + it supports cache (-:.
Requestify alternatives and similar modules
Based on the "HTTP" category.
Alternatively, view Requestify alternatives based on common mentions on social networks and blogs.
-
window.fetch polyfill
A window.fetch JavaScript polyfill. -
superagent
Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs. -
node-fetch
A light-weight module that brings the Fetch API to Node.js -
isomorphic-fetch
Isomorphic WHATWG Fetch API, for Node & Browserify -
node-android
Run Node.js on Android by rewrite Node.js in Java -
rocky
Full-featured, middleware-oriented, programmatic HTTP and WebSocket proxy for node.js (deprecated) -
global-agent
Global HTTP/HTTPS proxy agent configurable using environment variables. -
http-fake-backend
Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes. -
cacheable-request
Wrap native HTTP requests with RFC compliant cache support -
flashheart
A fully-featured Node.js REST client built for ease-of-use and resilience -
smoke
:dash: Simple yet powerful file-based mock server with recording abilities -
Bearer
Call any API and monitor requests with the Bearer API client for Node.js client Bearer.
Appwrite - The Open Source Firebase alternative introduces iOS support
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Requestify or a related project?
README
Requestify - Simplifies node HTTP request making. 
Requestify is a super easy to use and extendable HTTP client for nodeJS + it supports cache (-:.
Installation
npm install requestify
How to use?
Requestify is extremely easy to use and always return a promise (using the amazing Q module)...
Simply require the module and start requesting:
var requestify = require('requestify');
GET Request:
requestify.get('http://example.com').then(function(response) {
// Get the response body
response.getBody();
});
POST Request in JSON:
requestify.post('http://example.com', {
hello: 'world'
})
.then(function(response) {
// Get the response body (JSON parsed or jQuery object for XMLs)
response.getBody();
// Get the raw response body
response.body;
});
Configuration methods
requestify.setEncoding(encoding)
Sets Requestify's encoding. Requestify will use this encoding to decode the responses. Defaults to utf8.
requestify.setEncoding('utf8'); // utf8 is set by default anyway.
requestify.cacheTransporter(cacheTransporter)
Sets the cache transporter for Requestify's use. Requestify will use this transporter for caching the desired HTTP responses.
For using one of Requestify's core transporter use the core transporters object (coreCacheTransporters
).
PLEASE NOTE: the inMemory() transporter is set by default.
For example:
var coreCacheTransporters = requestify.coreCacheTransporters;
requestify.cacheTransporter(coreCacheTransporters.inMemory()); // set simple in-memory caching (set by default)
requestify.cacheTransporter(coreCacheTransporters.redis(myRedisInstance)); // Set the core Redis cache transporter, or
requestify.cacheTransporter(coreCacheTransporters.mongo(myMongooseInstance)); // set the core Mongo cache transporter
You can implement your own cache transporters (@see docs below)
requestify.redis(redisInstance) - @depercated
Sets Redis client instance. Requestify will use that instance for caching responses. Please note, Requestify will NOT cache anything by default and caching is allowed only for GET requests (see @cache options for further info).
PLEASE NOTE, this method is only a shorthand for using requestify.cacheTransporter(coreCacheTransporters.redis(myRedisInstance));
var redis = require('redis');
requestify.redis(redis.createClient());
API Reference
options
method {string}
HTTP method to use, can be any valid HTTP method (e.g. POST, GET, DELETE, HEAD, PUT, etc.).
body {object|string}
Can be either an object (key, val) or a string, will be formatted depending on the dataType property and served via request body.
params {object}
Object of key-value params, will be encoded to url encoded string and added as the request query string.
headers {object}
(key, value) object of headers (some headers like content-length are set by default)
cookies {object}
(key, value) object of cookies to encode and serve via the request header.
auth {{ username: string, password: string }}
Adds Basic authentication header with given username and password
dataType {string}
Determines the request data type (json|form-url-encoded), this option will encode the request body according to the given dataType and will add the appropriate header (defaults to json).
If null will be given, the body will be served as string.
timeout {number}
Set a timeout (in milliseconds) for the request.
redirect {boolean}
Determines if should continue with redirects
cache {{ cache: boolean, expires: number }}
Requistify has built-in Redis based caching mechanism. For using this feature, set the cache property to true using the following object:
requestify.get('http://examples.com/api/foo', {
cache: {
cache: true, // Will set caching to true for this request.
expires: 3600 // Time for cache to expire in milliseconds
}
});
Caching will always be set to false
by default.
requestify.request(url, options)
Executes a custom request according to options object
requestify.request('https://example.com/api/foo', {
method: 'POST',
body: {
foo: 'bar'
bar: 'foo'
},
headers: {
'X-Forwarded-By': 'me'
},
cookies: {
mySession: 'some cookie value'
},
auth: {
username: 'foo',
password: 'bar'
},
dataType: 'json'
})
.then(function(response) {
// get the response body
response.getBody();
// get the response headers
response.getHeaders();
// get specific response header
response.getHeader('Accept');
// get the code
response.getCode();
// get the raw response body
response.body;
});
requestify.get(url, options)
Executes a GET method request
requestify.get('http://example.com').then(function(response) {
// Get the response body
response.getBody();
});
requestify.post(url, data, options)
Executes a POST method request
requestify.post('http://example.com', {
hello: 'world'
})
.then(function(response) {
// Get the response body
response.getBody();
});
requestify.put(url, data, options)
Executes a PUT method request
requestify.put('http://example.com', 'some-file-content').then(function(response) {
// Get the response body
response.getBody();
});
requestify.delete(url, options)
Executes a DELETE method request
requestify.delete('http://example.com').then(function(response) {
// Get the response body
response.getBody();
});
requestify.head(url, options)
Executes a HEAD method request
requestify.head('http://example.com').then(function(response) {
// Get the response code
response.getCode();
});
Handling Errors
While the .then()
callback is used for handling succesful responses, the .fail()
callback is used for handling errors.
requestify.get('http://example.com')
.then(function(response) {
response.getCode(); // Some code between 200-299
})
.fail(function(response) {
response.getCode(); // Some error code such as, for example, 404
});
Custom Cache Transporters
Using Requestify, you can implement your own cache transporters for using currently unsupported stores. To implement your own store, all you have to do is implement Requestify's cache transporter interface. For example, you can see the core Redis transporter (./cache-transporters). Below is the interface specs to implement:
get(url: string, callback: function)
Returns the response according to the given URL from your cache store and call the given callback with the data.
set(url: string, response: {{ code: number, headers: object, body: string, created: timestamp }}, callback: function)
Store the given response by the given URL (key), please make sure to store the response object exactly in the same way you've got it.
purge(url: string, callback: function)
Purge the response according to the given URL (key) from your cache store.
Running Tests
To run the test suite first install the development dependencies:
$ npm install
Then run the tests:
$ npm test
License
The MIT License (MIT)
Copyright (c) 2013 Ran Mizrahi <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*Note that all licence references and agreements mentioned in the Requestify README section above
are relevant to that project's source code only.