axios/fetch实现stream流式请求示例详解

 更新时间:2023年09月20日 12:00:27   作者:天問  
这篇文章主要为大家介绍了axios/fetch实现stream流式请求示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

axios简介

axios 是一个支持node端和浏览器端的易用、简洁且高效的http库。本文主要介绍 axios / fetch 如何实现 stream 流式请求,注意这里需要区分 node 环境和浏览器环境。

node端

代码演示:

const axios = require('axios');
axios({
  method: 'get',
  url: 'http://tiven.cn/static/img/axios-stream-01-kcUzNdZO.jpg',
  responseType: 'stream'
})
.then(response => {
  response.data.on('data', (chunk) => {
    // 处理流数据的逻辑
  });
  response.data.on('end', () => {
    // 数据接收完成的逻辑
  });
}); 

浏览器端

在浏览器端,axios 是使用 XMLHttpRequest 对象来实现请求,设置 responseType: 'stream' 后会出现以下警告⚠️:

The provided value 'stream' is not a valid enum value of type XMLHttpRequestResponseType.

所以,在浏览器端,我们需要使用浏览器内置API fetch 来实现 stream 流式请求。

代码演示:

async function getStream() {
  try {
    let response = await fetch('/api/admin/common/testStream');
    console.log(response);
    if (!response.ok) {
      throw new Error('Network response was not ok');
    }
    const reader = response.body.getReader();
    const textDecoder = new TextDecoder();
    let result = true;
    let output = ''
    while (result) {
      const { done, value } = await reader.read();
      if (done) {
        console.log('Stream ended');
        result = false;
        break;
      }
      const chunkText = textDecoder.decode(value);
      output += chunkText;
      console.log('Received chunk:', chunkText);
    }
  } catch (e) {
    console.log(e);
  }
}

以上就是axios / fetch 实现 stream 流式请求的详细内容,更多关于axios / fetch 实现 stream 流式请求的资料请关注脚本之家其它相关文章!

相关文章

最新评论