Search code examples
javascriptnode.jsresponseunirest

unirest.post is not a function


I am trying to call my api through Node.js using Unirest.

I am getting an error which is :

unirest.post is not a function

My code is as follows:

var unirest = require(['unirest'], function (unirest) { });
unirest.post('http://localhost:8080/country')
  .headers({ 'Accept': 'application/json', 'Content-Type': 'application/json' })
  .send(json_query)
  .end(function (response) {
    alert(response.body);
  });

Can someone explain me why does it occur?


Solution

  • OK. So, with the additional information, you need to use async imports as per link you provided:

    require(['foo'], function (foo) {
        // foo is available here...
    });
    // foo isn't available here
    

    And in your specific case,

    require(['unirest'], function (unirest) {
        unirest.post('http://localhost:8080/country') // etc.
    });