Javascript 如何使用指针或请求库在Node.js中上载包含PUT请求的二进制体
我试图在amazon预签名上传url上编写一个单元测试,该url接受一个原始二进制体的PUT请求 查看针库和请求库的示例,它们都使用表单数据示例。有人能给我举一个例子,使用任何一个库在请求体中发送一个本地文件作为原始二进制文件吗 请求库 针库 我使用PUT请求发送二进制体,但您可以使用请求库或针库修改此代码Javascript 如何使用指针或请求库在Node.js中上载包含PUT请求的二进制体,javascript,node.js,node-request,needle.js,Javascript,Node.js,Node Request,Needle.js,我试图在amazon预签名上传url上编写一个单元测试,该url接受一个原始二进制体的PUT请求 查看针库和请求库的示例,它们都使用表单数据示例。有人能给我举一个例子,使用任何一个库在请求体中发送一个本地文件作为原始二进制文件吗 请求库 针库 我使用PUT请求发送二进制体,但您可以使用请求库或针库修改此代码 // Don't put encoding params in readFile function ! fs.readFile("example.torrent", function (
// Don't put encoding params in readFile function !
fs.readFile("example.torrent", function (err, file) {
if (err) {
throw err;
}
var r = request({
method: 'PUT',
uri: 'https://api.mywebsite.com/rest/1.0/addFile',
headers: {
"Content-Type": "application/octet-stream" // Because binary file
},
json: true // Because i want json response
});
r.body = file; // Put the file here
r.then(function (response) {
console.log("success", response);
}).catch(function (error) {
console.log("error", error);
});
});
您只需设置
内容类型
标题
使用axios
lib
return axios({
method: "put",
url: your_url,
headers: { "Content-Type": "application/octet-stream" },
data: Buffer.from(blob.data)
});
其中bolb
为文件类型(req.body.files)
return axios({
method: "put",
url: your_url,
headers: { "Content-Type": "application/octet-stream" },
data: Buffer.from(blob.data)
});