为什么请求AI的API请求在Apifox中流式输出但在浏览器中一次性输出?

2026-06-09 1 阅读 linjiahao852
相同的AI请求,为什么用apifox看是按照时间流式输出的,但是在浏览器上面看请求的EventStream就是在同一个时间输出的这是前端请求的代码 if ('fetch' in window) { // Use fetch API response = wait fetch('/v1/workflows/run', { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ "inputs": { "input": "商品怎么配置" }, "response_mode": "streaming" }), }) } const reader = response.body.getReader(); const 解码器 = new TextDecoder(); while (true) { const { 完成,值 } =等待 reader.read(); console.log('接收块:', new Date().getTime()); // 看时钟是否分散 if (done) break; const chunk = detector.decode(value, { Stream: true }); console.log("lines==>", chunk) constlines = chunk.split("\n").filter(line => line.startsWith("data:")); for (const line oflines) { const data = line.replace("data: ", "").trim();如果(数据===“[完成]”)返回; // 尝试 { // const json = JSON.parse(data); // const text = json.choices?.[0]?.delta?.content // OpenAI 格式 // ?? json.delta?.text; // Anthropic 格式 // if (text) process.stdout.write(text); // 或更新 UI // } catch {} } }