我想批量转运,因此我上传了.csv文件,如果我使用curl命令,它对我有用,但是当我使用其on节点代码时,它向我显示错误“文件未上传”
关于卷曲,我正在使用对我有用的代码:-
curl -F 'data=@/var/www/html/achupload.csv' https://sandbox.usaepay.com/api/v2/bulk_transactions -H "Authorization: Basic X3llMVI3Mk9PdzFHOXBqcW1GaVp2NHJINjRc="
这是我的节点代码,向我显示错误消息:-
var request = require('request');
var headers = {
'Authorization': 'Basic X3llMVI3Mk9PdzFHOXBqcW1GaVp2NHJINjRc='
};
var dataString = '@/var/www/html/achupload.csv';
var options = {
url: 'url',
method: 'POST',
headers: headers,
data: dataString
};
console.log("options====",options);
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
else{
console.log("notttttttttttt================",response.body);
}
}
request(options, callback);
答案 0 :(得分:1)
此修改如何?
fs.createReadStream()
检索文件内容。formData
的形式发送。var request = require('request');
var headers = {
'Authorization': 'Basic ###'
};
var dataString = '@/var/www/html/achupload.csv';
var options = {
url: 'url',
method: 'POST',
headers: headers,
data: dataString
};
至:
var fs = require('fs'); // Added
var request = require('request');
var headers = {
'Authorization': 'Basic ###'
};
var dataString = {data: fs.createReadStream('/var/www/html/achupload.csv')}; // Modified
var options = {
url: 'url',
method: 'POST',
headers: headers,
formData: dataString // Modified
};
'Authorization': 'Basic ###'
。请注意这一点。使用此功能时,请将###
替换为您的内容。如果这不能解决您的问题,我深表歉意。