You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js后端发送HTTPS GET请求报ENOTFOUND错误如何解决?

问题修复方案

你代码里有两处配置错误,修正后即可正常运行:

  • hostname参数填写错误
    https.request的hostname配置仅需要填写纯域名,不能带https://协议前缀,你填了带前缀的地址,导致DNS解析时无法识别域名,触发ENOTFOUND报错,正确写法是hostname: 'api.spotify.com'。
  • GET请求参数传递逻辑错误
    你当前发起的是GET请求,不需要把offset参数序列化成JSON写入请求体,GET请求的参数需要拼接在path的查询字符串位置,也不需要调用req.write()写入请求体。

修正后的完整代码如下:

const https = require('https');

// 获取access token
let accessToken = await spotifyAuth.getAccessToken();

const options = {
  hostname: 'api.spotify.com',
  // offset参数直接拼在路径的查询串中
  path: '/v1/artists/1XqqyIQYMonHgllb1uysL3/albums?offset=0',
  method: 'GET',
  headers: {
    'Authorization': 'Bearer ' + accessToken,
  },
}

const req = https.request(options, res => {
  console.log(`proxyiprequest() statusCode: ${res.statusCode}`);
  // 拼接返回结果的逻辑可以按需补充,示例直接打印
  let result = '';
  res.on('data', chunk => {
    result += chunk;
  });
  res.on('end', () => {
    console.log(JSON.parse(result));
  });
})

req.on('error', error => {
  console.error('proxyiprequest() err=', error);
})

// GET请求无需写入请求体,直接结束请求即可
req.end();

内容的提问来源于stack exchange,提问作者Martin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 23:06:01