Node.js


Installing & Making a Request

To utilize unirest for node.js install the the npm module:

Shell
$ npm install unirest

After installing the npm package, you can now create a new instance of the unirest client and form a request.

JavaScript
var unirest = require('unirest');

unirest.post(API_URL)
  .header("X-Mashape-Key", API_KEY)
  .end(function (result) {
    console.log(result.status, result.headers, result.body);
  });
Uploading Files
JavaScript
unirest.post(API_URL)
  .header("X-Mashape-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
JavaScript
unirest.get(API_URL)
  .header("X-Mashape-Key", API_KEY)
  .end(function (result) {
    console.log(result.status, result.headers, result.body);
  });
PUT
JavaScript
unirest.put(API_URL)
  .header("X-Mashape-Key", API_KEY)
  .end(function (result) {
    console.log(result.status, result.headers, result.body);
  });
POST
JavaScript
unirest.post(API_URL)
  .header("X-Mashape-Key", API_KEY)
  .end(function (result) {
    console.log(result.status, result.headers, result.body);
  });
PATCH
JavaScript
unirest.patch(API_URL)
  .header("X-Mashape-Key", API_KEY)
  .end(function (result) {
    console.log(result.status, result.headers, result.body);
  });
DELETE
JavaScript
unirest.delete(API_URL)
  .header("X-Mashape-Key", API_KEY)
  .end(function (result) {
    console.log(result.status, result.headers, result.body);
  });