Unirest
Installing & Making a Request
To utilize Unirest for node.js install the npm module:
$ npm install unirest
After installing the npm package, you can now create a new instance of the Unirest client and form a request.
var unirest = require('unirest');
unirest.post(API_URL)
.header("X-RapidAPI-Key", API_KEY)
.end(function (result) {
console.log(result.status, result.headers, result.body);
});
Uploading a File
unirest.post(API_URL)
.header("X-RapidAPI-Key", API_KEY)
.header({'Content-Type': 'multipart/form-data'})
.field('parameter', 'value') // Form field
.attach('file', '/path/to/file') // Attachment
.end(function (result) {
console.log(result.status, result.headers, result.body);
});
Unirest Functionality
A request can be initiated by invoking the appropriate method on the Unirest object, then calling .end() to send the request. Alternatively, you can send the request directly by providing a callback along with the URL.
GET
unirest.get(API_URL)
.header("X-RapidAPI-Key", API_KEY)
.end(function (result) {
console.log(result.status, result.headers, result.body);
});
PUT
unirest.put(API_URL)
.header("X-RapidAPI-Key", API_KEY)
.end(function (result) {
console.log(result.status, result.headers, result.body);
});
POST
unirest.post(API_URL)
.header("X-RapidAPI-Key", API_KEY)
.end(function (result) {
console.log(result.status, result.headers, result.body);
});
PATCH
unirest.patch(API_URL)
.header("X-RapidAPI-Key", API_KEY)
.end(function (result) {
console.log(result.status, result.headers, result.body);
});
DELETE
unirest.delete(API_URL)
.header("X-RapidAPI-Key", API_KEY)
.end(function (result) {
console.log(result.status, result.headers, result.body);
});
Learn more about how to use an API with JavaScript.
Updated over 5 years ago