0

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?

Sammy
  • 77
  • 2
  • 9
  • You haven't specified any headers. How are you supposed to log in then? I am guessing if you spit out the headers on the response object in the console you would see something like a HTTP 400 Forbidden response. – oligofren Aug 27 '17 at 07:10
  • @oligofren thanks for pointing it out, that API worked by setting the header, however I have edited my question and this API still gives an empty response even after setting the header, can you please help me! – Sammy Aug 27 '17 at 09:45

1 Answers1

0

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.

oligofren
  • 20,744
  • 16
  • 93
  • 180