【问题标题】:NodeJS request get return me html code instead of jsonNodeJS请求得到返回我的html代码而不是json
【发布时间】:2021-08-10 12:26:04
【问题描述】:

我正在尝试从 get 请求中获取 json,它在 python 中工作,但在 NodeJs 中显示页面的 html 代码源 这是我的代码:


app.get("/well", function(request, response)  {
  const req = require('request');

  const options = {
      url: 'https://swarmmanager.francecentral.cloudapp.azure.com:3000',
      method: 'GET',
      headers: {
          'Accept': 'application/json',
      },
      agentOptions: {
        ca: fs.readFileSync("public/IdaktoPKIRootCA.crt")
      }
  };

  req(options, function(err, res, body) {
      console.log(body);
  });
});

这是另一个版本但同样的问题:

app.get("/well", function(request, response)  {
  g_CnieOidcAddr = 'https://swarmmanager.francecentral.cloudapp.azure.com:3000';
  const options = {
    hostname: 'swarmmanager.francecentral.cloudapp.azure.com',
    port: 3000,
    method: 'GET',
    headers: {
        'Accept': 'application/json',
    },
    ca: fs.readFileSync("public/IdaktoPKIRootCA.crt")
};
  const req = https.request(options, (res) => {
    console.log('statusCode:', res.statusCode);
    console.log('headers:', res.headers);
  
    res.on('data', (d) => {
      process.stdout.write(d);
    });
  });
  
  req.on('error', (e) => {
    console.error(e);
  });
  req.end();
});

我尝试在 python 中执行此操作,找到返回 json 的工作:

headers = {'Accept': 'application/json'}
                r = requests.get(g_CnieOidcAddr + '/.well-known/openid-configuration', params={}, headers = headers, verify='./IdaktoPKIRootCA.crt')
                print (r.text)

如果有人知道我是一个接受者^^感谢阅读。

【问题讨论】:

    标签: node.js json express get request


    【解决方案1】:

    好吧,工作发现我只是在 url 的末尾忘记了一些东西,所以如果你来到这个页面,2 个代码可以找到一个请求

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-12
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      相关资源
      最近更新 更多