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

类中使用node-client-ntlm的async函数Promise返回数据不完整问题

问题解决思路

1. 修复async函数的返回逻辑

你现在的代码同时混用了await和.then(),而且.then()里只做了日志输出却没返回响应对象,导致const response拿到的是undefined,根本没法返回完整数据。改成try/catch结构更清晰,也能正确获取并返回完整响应:

async callCurl(postFields) {
  this.postFields = postFields;
  try {
    const response = await client.request(
      {
        url: this.url,
        method: "POST",
        debug: true,
        body: `folderGuid=${this.postFields}`,
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
        resolve: true,
      },
      "username",
      "password",
      "",
      "domain"
    );
    console.log(response.body);
    return response; // 正确返回完整响应对象
  } catch (error) {
    console.error(error);
    throw error; // 抛出错误让调用方处理
  }
}

2. 解决响应截断问题

如果修复返回逻辑后还是只能拿到部分数据,大概率是node-client-ntlm处理响应时没完整读取流数据,可以试试这些操作:

  • 确认resolve: true参数是否生效:这个参数本该让库自动解析完整响应,要是不生效,就手动处理响应流。
  • 调整请求头:添加"Accept-Encoding": "identity"禁用压缩,避免服务器返回压缩数据但库没正确解压导致截断。
  • 查看debug日志:你已经开了debug: true,仔细看日志里的响应长度、传输状态,确认是服务器只返回了部分数据,还是客户端没接收完整。

如果要手动处理响应流,把resolve: false,自己监听data事件收集数据:

async callCurl(postFields) {
  this.postFields = postFields;
  return new Promise((resolve, reject) => {
    const req = client.request(
      {
        url: this.url,
        method: "POST",
        debug: true,
        body: `folderGuid=${this.postFields}`,
        headers: {
          "content-type": "application/x-www-form-urlencoded",
          "Accept-Encoding": "identity" // 禁用压缩
        },
        resolve: false, // 不自动解析,手动处理流
      },
      "username",
      "password",
      "",
      "domain"
    );

    let rawData = '';
    req.on('data', (chunk) => {
      rawData += chunk;
    });

    req.on('end', () => {
      try {
        // 如果返回JSON,这里可解析:const responseData = JSON.parse(rawData);
        resolve(rawData);
      } catch (e) {
        reject(e);
      }
    });

    req.on('error', reject);
  });
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 02:45:33