【问题标题】:Not getting a response when I should be getting it from API?当我应该从 API 获取响应时没有得到响应?
【发布时间】:2017-08-26 10:31:15
【问题描述】:

我正在使用 mocha 来获取此 API 的响应:“http://reqres.in/api/users?page=1

当我在 POSTMAN 中使用此请求时,我收到了响应:

但是,当在 mocha 中使用相同的请求来获得响应时(在这种情况下是令牌)。我得到一个空的回复。 以下是我编写的代码和终端中的输出:

    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);

    });

谁能帮帮我,我做错了什么?

【问题讨论】:

  • 您没有指定任何标题。那你应该怎么登录?我猜如果你在控制台的响应对象上吐出标题,你会看到类似 HTTP 400 Forbidden 响应。
  • @oligofren 感谢您指出这一点,该 API 通过设置标题来工作,但是我已经编辑了我的问题,即使在设置标题后,这个 API 仍然给出一个空响应,你能帮帮我吗!

标签: api mocha.js supertest


【解决方案1】:

我坐在地铁上查看响应时对此进行了调试。打开这个running example on RunKit。展开响应对象并查看标头。您将看到重定向到 HTTPS 版本 (HTTP 301)。这就是它在浏览器中工作的原因。因此,只需将 http 替换为 https。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 2019-04-12
    • 2013-11-08
    相关资源
    最近更新 更多