AlfieriChou / alfierichou.github.io

AlfieriChou的博客
https://alfierichou.com
4 stars 0 forks source link

请求库请求stream资源的异同 #19

Open AlfieriChou opened 2 years ago

AlfieriChou commented 2 years ago
  1. got
const ret = got.stream(url)
console.log(ret)

获取到的资源,缺乏相应的请求头文件信息(不能直接用于form-data,当做文件用。PS: 通过本地debug源码发现,form-data对于本地文件会获取文件信息的length或者是远程文件headers里边的content-length) image

官方issue存在这个问题 https://github.com/sindresorhus/got/issues/1864

  1. axios
const ret = await axios(url, {
  responseType: 'stream'
})
console.log(ret.data)

获取到的资源存在远程资源的headers详细信息

image

通过两个请求库的对比,发现got获取的是原生的stream,而axios获取的是携带了头部信息的流。那么,第一种方式则需要通过stream存储到本地,然后,本地文件才与axios获取的stream是一致的。

AlfieriChou commented 2 years ago

查看form-data源码发现 form-data存在,append文件的时候,添加response流头部的content-length信息或者是文件fileSize image