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

API挑战求助:循环PUT请求无法达成total_diff>500ms要求

问题

我正在进行一项API挑战,步骤如下:

  • 向http://challenge.com/challenges?nickname=anythinghere发送POST请求,获取包含id、actives_at、called_at、total_diff的响应;
  • 将获取到的id放入请求头X-Challenge-Id中,向http://challenge.com/challenges发送PUT请求,需循环调用直到total_diff超过500ms;
  • 成功完成后会返回包含attempts和url的result响应。

但我的代码仅能发起1-2次PUT请求,导致挑战失败,返回的url为undefined,而朋友可发起2000次请求。以下是我的JavaScript代码:

async function intervalCall() {
  try {
    const startResponse = await fetch(
      "http://example/challenges",
      { method: "POST" }
    ); //make the initial fetch call to start the challenge
    const dataRes = await startResponse.json();
    console.log(dataRes);

    // {
    //   id: 'unique ID',
    //   actives_at: 1700570652300,
    //   called_at: 1700570651628,
    //   total_diff: 0
    // }

    let dataResId = dataRes.id;
    let currentInterval = dataRes.total_diff;

    if (currentInterval <= 500) {
      const startResponsePut = await fetch(
        "http://example/challenges",
        { method: "PUT", headers: { "X-Challenge-Id": dataResId } }
      ); //make the PUT call with the challenge id untill the differrence is >500
      const responsePutJson = await startResponsePut.json();
      console.log(responsePutJson);
      currentInterval += dataRes.total_diff;
    } else {
      console.log("Challenge interval exceeds 500");
    }
  } catch (error) {
    console.log("Error", error);
  }
}

// Call the function
intervalCall();

问题分析与解决方案

问题点

  1. 缺少循环逻辑:原代码仅用if判断执行一次PUT请求,没有循环机制持续调用直到total_diff超过500ms,所以只会发起1次请求就结束。
  2. 错误更新total_diff:更新currentInterval时使用的是初始POST请求返回的dataRes.total_diff,而非每次PUT请求返回的最新total_diff,导致数值无法正确累加。
  3. 请求URL错误:初始POST请求的URL应为http://challenge.com/challenges?nickname=anythinghere,原代码使用了错误的http://example/challenges。

修正后的代码

async function intervalCall() {
  try {
    // 修正初始POST请求的URL,添加nickname参数
    const startResponse = await fetch(
      "http://challenge.com/challenges?nickname=anythinghere",
      { method: "POST" }
    );
    const dataRes = await startResponse.json();
    console.log("初始响应:", dataRes);

    let challengeId = dataRes.id;
    let currentTotalDiff = dataRes.total_diff;

    // 使用while循环持续发起PUT请求,直到total_diff超过500
    while (currentTotalDiff <= 500) {
      const putResponse = await fetch(
        "http://challenge.com/challenges",
        { 
          method: "PUT", 
          headers: { "X-Challenge-Id": challengeId } 
        }
      );
      const putData = await putResponse.json();
      console.log("PUT响应:", putData);

      // 检查是否已收到成功响应(包含result字段)
      if (putData.result) {
        console.log("挑战完成:", putData.result);
        break;
      }

      // 更新为最新的total_diff值
      currentTotalDiff = putData.total_diff;
    }

    if (currentTotalDiff > 500) {
      console.log("total_diff已超过500ms");
    }
  } catch (error) {
    console.error("请求出错:", error);
  }
}

intervalCall();

关键修改说明

  • 替换循环逻辑:将if改为while循环,确保只要total_diff未超过500就持续发起PUT请求。
  • 正确更新数值:每次PUT请求后,用返回的putData.total_diff更新currentTotalDiff,保证判断条件基于最新数据。
  • 修正请求URL:将初始POST和PUT请求的URL改为挑战指定的http://challenge.com地址,添加必要的nickname参数。
  • 成功响应判断:增加对result字段的检查,一旦收到成功响应就提前终止循环,避免不必要的请求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 17:02:52