Search code examples
apimocha.jssupertest

Not getting a response when I should be getting it from API?


I am using mocha for getting the response of this API: "http://reqres.in/api/users?page=1"

I am getting a response when I use this request in POSTMAN: enter image description here

However when using the same request in mocha to get a response (token in this case). I am getting an empty reponse. Below is the code I wrote and the output in Terminal:

    const mocha = require ('mocha');
    const should = require ('should');
    const supertest = require('supertest');


   let baseUrl = supertest("http://reqres.in/");
   let listUserEndPoint = 'api/users?page=1';

    it("List User", async function() {
    let res = await baseUrl.get(listUserEndPoint)
         .set('Content-Type',  'application/json') // Setting the content type as header
    console.log(res.body);

    });

Terminal ouptut

Can someone help me out, what I am doing wrong?


Solution

  • I debugged this while sitting on the subway and looking at the response. Open this running example on RunKit. Expand the response object and look at the headers. You'll see that you get a redirect to the HTTPS version (HTTP 301). That's why it works in the browser. So just replace http with https.