Node.js Node JS通过HTTP上传文件流
我正在将我的一个项目从Node.js Node JS通过HTTP上传文件流,node.js,request,fetch,axios,Node.js,Request,Fetch,Axios,我正在将我的一个项目从request切换到更轻量级的项目(例如got、axios或fetch)。一切都进展顺利,但是,我在尝试上载文件流时遇到了问题(PUT和POST)。它可以与请求包一起工作,但其他三个包中的任何一个都会从服务器返回500 我知道500通常意味着服务器端出现问题,但它仅与我正在测试的HTTP包一致。当我将代码还原为使用请求时,它工作正常 这是我当前的请求代码: Request.put(`http://endpoint.com`, { headers: { Autho
request
切换到更轻量级的项目(例如got、axios或fetch)。一切都进展顺利,但是,我在尝试上载文件流时遇到了问题(PUT
和POST
)。它可以与请求包一起工作,但其他三个包中的任何一个都会从服务器返回500
我知道500通常意味着服务器端出现问题,但它仅与我正在测试的HTTP包一致。当我将代码还原为使用请求
时,它工作正常
这是我当前的请求代码:
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);
});
下面是使用另一个包的尝试之一(在本例中为get):
根据got文档,我还尝试根据其示例将表单数据
包与之结合使用,但仍然遇到相同的问题
我能收集到的这两个数据之间的唯一区别是使用get
我必须手动指定内容类型
标题,否则端点会在这方面给我一个正确的错误。否则,我不确定这两个包是如何用流构造主体的,但正如我所说,fetch
和axios
也会产生与got
完全相同的错误
如果您想使用
fetch
或axios
获取任何代码片段,我也很乐意发布它们。看起来这是一个标题问题。如果我直接使用FormData
(即标题:form.getHeaders()
)中的标题,然后添加额外的标题(Authorization
),那么这就可以正常工作。看起来这是一个标题问题。如果我直接使用FormData
(即headers:form.getHeaders()
)中的头文件,然后再添加额外的头文件(Authorization
),那么这就可以很好地解决问题。我知道这个问题是不久前提出的,但我也错过了简单的问题
为了从当前的库中找到类似的特性,我们不得不做一些实验
不幸的是,我还没有使用过“get”,但我希望下面的两个示例能够帮助那些对使用本机/库或流行库感兴趣的人
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
的用法,它被附加到Axios配置中的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);
})();
我知道这个问题是不久前提出的,但我也错过了简单的问题 为了从当前的库中找到类似的特性,我们不得不做一些实验 不幸的是,我还没有使用过“get”,但我希望下面的两个示例能够帮助那些对使用本机/库或流行库感兴趣的人
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
的用法,它被附加到Axios配置中的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);
})();
不确定这是否有帮助,但根据GET文档,您需要传递一个FormData作为对象的主体-我也尝试过这样做。使用
表单数据
包,我用'content'和流信息调用了append
,但仍然得到了相同的问题。我将编辑我的问题以反映这一点。不确定这是否有帮助,但根据所获得的文档,您需要将FormData作为对象的主体传递-我也尝试过这一点。使用表单数据
包,我用'content'和流信息调用了append
,但仍然得到了相同的问题。我将编辑我的问题以反映这一点。感谢您提供Axios解决方案。响应类型“stream”在用作多部分POST时工作正常感谢您提供的Axios解决方案。当用作多部分POST时,响应类型“stream”可以正常工作