我正在尝试使用V2端点进行文件上传。我相信内容标题设置正确,但我不断收到此错误。有人可以帮忙吗?
const request = require('request');
var fs = require('fs');
var apiArgs = '{ "path" : "/testfolder/Nespresso.zip", "mode" : "add", "autorename" : true, "mute" : false }' ;
var formData = {
'data-binary': fs.createReadStream("F:\\Nespresso.zip")
};
const options = {
headers: {
'Authorization' : 'Bearer ############',
'Content-Type' : 'application/octet-stream',
'Dropbox-API-Arg': apiArgs
},
formData : formData
};
request.post('https://content.dropboxapi.com/2/files/upload', options, function(err, res, body) {
//let json = JSON.parse(body);
console.log(body);
if(res)
console.log(res.statusCode);
if(err)
console.log(err);
});
节点错误:
调用API函数“files / upload”时出错:错误的HTTP“Content-Type”标题:“m ultipart /格式数据;边界= -------------------------- 298294176382492406791283" 。期待“application / octet-stream”之一,“text / plain; charset = dropbox-cor 窝棚”。 400
答案 0 :(得分:1)
不确定data-binary
是否是请求配置对象的有效formData
属性。见https://github.com/request/request#multipartform-data-multipart-form-uploads。除此之外,Dropbox还需要application/octet-stream
类型数据,而不是您提供的multipart/form-data
。怎么样:
const request = require('request');
var fs = require('fs');
const options = {
url: 'https://content.dropboxapi.com/2/files/upload',
headers: {
'Authorization' : 'Bearer ############',
'Content-Type' : 'application/octet-stream',
'Dropbox-API-Arg': JSON.stringify({
'path' : '/package.json'
})
}
};
const uploadStream = request.post(options, function(err, res, body) {
//let json = JSON.parse(body);
console.log(body);
if(res)
console.log(res.statusCode);
if(err)
console.log(err);
});
fs.createReadStream('./package.json').pipe(uploadStream);