ale*_*ips 1 request fetch node.js axios
我正在将我的一个项目从request
over切换到更轻量级的项目(例如 got、axios 或 fetch)。一切都进行得很顺利,但是,我在尝试上传文件流 (PUT
和POST
)时遇到了问题。它与请求包一起工作正常,但其他三个中的任何一个从服务器返回 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);
});
Run Code Online (Sandbox Code Playgroud)
这是使用另一个包的尝试之一(在这种情况下,得到了):
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);
});
Run Code Online (Sandbox Code Playgroud)
根据获得的文档,我还尝试form-data
根据其示例将该包与它结合使用,但我仍然遇到相同的问题。
我可以收集到的这两个之间的唯一区别是got
我必须手动指定Content-Type
标头,否则端点确实会给我一个正确的错误。否则,我不确定这 2 个包是如何用流构建主体的,但正如我所说,fetch
并且axios
也产生与got
.
如果您想要使用任何片段,fetch
或者axios
我也很乐意发布它们。
小智 6
我知道这个问题是不久前被问到的,但我也缺少请求包中的简单管道支持
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"))
Run Code Online (Sandbox Code Playgroud)
并且不得不做一些实验来从当前的库中找到类似的功能。
不幸的是,我没有使用过“got”,但我希望以下 2 个示例可以帮助其他有兴趣使用 Native http / https库或流行的axios库的人
支持管道!
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"],
},
})
)
Run Code Online (Sandbox Code Playgroud)
请注意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);
})();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2654 次 |
最近记录: |