2

In NodeJS using Unirest http library i am automating the Rest Apis. Currently i am stuck on how to pass the query parameters with rest url.

i have tried following solutions but non is working: solution 1

unirest('GET', 'https://my-domain.com/Api/learner/courses')
.header({
        "content-type": "application/json",
        "authorization": token
    })
.queryString("pageNumber", "1")
.queryString("sortDirection", "ASC")
.queryString("status", "all")
.end(function (response) {
response.status.should.be.equal(200);
});

got the following error on above execution:

TypeError: unirest(...).header(...).queryString is not a function

solution 2:

unirest('GET', 'https://my-domain.com/Api/learner/courses{pageNumber}{sortDirection}{status}')
.header({
        "content-type": "application/json",
        "authorization": token
    })
.routeParam("pageNumber", "1")
.routeParam("sortDirection", "ASC")
.routeParam("status", "all")
.end(function (response) {
response.status.should.be.equal(200);
});

got error:

TypeError: unirest(...).header(...).routeParam is not a function

solution 3:

const param = {
        pageNumber: 1,
        sortDirection: "ASC",
        status: "all"}

unirest('GET', 'https://my-domain.com/Api/learner/courses{pageNumber}{sortDirection}{status}') .header({ "content-type": "application/json", "authorization": token }) .send(param) .end(function (response) { response.status.should.be.equal(200); });

got error:

Uncaught Error: Error: got 500 response

Any help would be much appriciated! thanks.

Asad
  • 49
  • 1
  • 3
  • 9

1 Answers1

1

queryString is not a method defined in unirest it is query.

See here unirest npm

unirest('GET', 'https://my-domain.com/Api/learner/courses')
.header({
    "content-type": "application/json",
    "authorization": token
})
.query("pageNumber", "1")
.query("sortDirection", "ASC")
.query("status", "all")
.end(function (response) {
     response.status.should.be.equal(200);
});

This is the documentation

unirest
 .post('http://mockbin.com/request')
 .query('name=nijiko')
 .query({
    pet: 'spot'
  })
 .then((response) => {
    console.log(response.body)
 });

You can try this version

3960278
  • 756
  • 4
  • 12
  • thanks @Suryapratap Singh for the answer. i tried your solution but still get the error "TypeError: unirest(...).header(...).query is not a function". then i study the link you mentioned and reached the solution. so i just pass the parameter object to .qs() and it works. thanks – Asad Mar 02 '20 at 11:46