1

我正在将我的一个项目从requestover 切换到更轻量级的项目(例如 got、axios 或 fetch)。一切都很顺利,但是,我在尝试上传文件流(PUTPOST)时遇到了问题。它适用于请求包,但其他三个中的任何一个都从服务器返回 500。

我知道 500 通常意味着服务器端的问题,但它仅与我正在测试的 HTTP 包一致。当我恢复我的代码使用request时,它工作正常。

这是我当前的请求代码:

Request.put(`http://endpoint.com`, {
  headers: {
    Authorization: `Bearer ${account.token.access_token}`
  },
  formData: {
    content: fs.createReadStream(localPath)
  }
}, (err, response, body) => {
  if (err) {
    return callback(err);
  }

  return callback(null, body);
});

这是使用另一个包的尝试之一(在这种情况下,得到了):

got.put(`http://endpoint.com`, {
  headers: {
    'Content-Type': 'multipart/form-data',
    Authorization: `Bearer ${account.token.access_token}`,
  },
  body: {
    content: fs.createReadStream(localPath)
  }
})
  .then(response => {
    return callback(null, response.body);
  })
  .catch(err => {
    return callback(err);
  });

根据获取的文档,我还尝试form-data根据其示例将包与它结合使用,但我仍然遇到同样的问题。

我可以收集的这两个之间的唯一区别是got我必须手动指定Content-Type标头,否则端点确实会给我一个适当的错误。否则,我不确定这 2 个包是如何使用流构建主体的,但正如我所说,fetch并且axios也会产生与got.

如果您想要使用任何片段,fetch或者axios我也很乐意发布它们。

4

3 回答 3

7

我知道这个问题是不久前被问到的,但我也错过了请求包中的简单管道支持

const request = require('request');

request
  .get("https://res.cloudinary.com/demo/image/upload/sample.jpg")
  .pipe(request.post("http://127.0.0.1:8000/api/upload/stream"))


// Or any readable stream
fs.createReadStream('/Users/file/path/localFile.jpeg')
  .pipe(request.post("http://127.0.0.1:8000/api/upload/stream"))

并且必须做一些实验才能从当前库中找到类似的功能。

不幸的是,我没有使用过“got”,但我希望以下 2 个示例可以帮助对使用 Native http / https库或流行的axios库感兴趣的其他人


HTTP/HTTPS

支持管道!

const http = require('http');
const https = require('https');

console.log("[i] Test pass-through: http/https");

// Note: http/https must match URL protocol
https.get(
  "https://res.cloudinary.com/demo/image/upload/sample.jpg",
  (imageStream) => {
    console.log("    [i] Received stream");

    imageStream.pipe(
      http.request("http://localhost:8000/api/upload/stream/", {
        method: "POST",
        headers: {
          "Content-Type": imageStream.headers["content-type"],
        },
      })
    );
  }
);

// Or any readable stream
fs.createReadStream('/Users/file/path/localFile.jpeg')
  .pipe(
    http.request("http://localhost:8000/api/upload/stream/", {
      method: "POST",
      headers: {
        "Content-Type": imageStream.headers["content-type"],
      },
    })
  )

爱讯

请注意Axios 配置中的imageStream.data和 附加到的用法。data

const axios = require('axios');

(async function selfInvokingFunction() {
  console.log("[i] Test pass-through: axios");

  const imageStream = await axios.get(
    "https://res.cloudinary.com/demo/image/upload/sample.jpg",
    {
      responseType: "stream", // Important to ensure axios provides stream
    }
  );

  console.log("  [i] Received stream");

  const upload = await axios({
    method: "post",
    url: "http://127.0.0.1:8000/api/upload/stream/",
    data: imageStream.data,
    headers: {
      "Content-Type": imageStream.headers["content-type"],
    },
  });

  console.log("Upload response", upload.data);
})();
于 2020-09-22T01:58:22.900 回答
2

看起来这是一个标题问题。如果我直接从FormData(即headers: form.getHeaders())使用标题并在之后添加我的附加标题(Authorization),那么这最终工作得很好。

于 2016-06-25T00:52:13.807 回答
0

对我来说,当我在 FormData 上添加其他参数时才有效。

const form = new FormData();
form.append('file', fileStream);

const form = new FormData();
form.append('file', fileStream, 'my-whatever-file-name.mp4');

这样我就可以将流从我的后端发送到节点中的另一个后端,等待 multipart/form-data 中称为“文件”的文件

于 2021-06-30T19:26:54.783 回答