Pra*_*kar 3 javascript fs node.js axios
尝试使用 axios 执行 POST 请求时出现此错误:
TypeError: data should be a string, Buffer or Uint8Array
Run Code Online (Sandbox Code Playgroud)
这是我的代码片段:
var fs = require('fs'),
axios = require('axios');
var FormData = require('form-data');
var form = new FormData();
form.append('file', fs.createReadStream("qa_test_file_DOC.txlf"));
form.append('extractArchive', false);
let request_config = {
headers: {
'Authorization': `Bearer eyJhbGciOiJIUzI1NXXXX.....`,
...form.getHeaders()
}
}
let reqUrl = "https://XXXXX/XX/rest/v1/XXXXX";
try {
axios.post(reqUrl, form, request_config)
.then(function (response) {
console.log(response);
return callback(response);
})
.catch(function (error) {
console.log(error);
return callback(error);
});
} catch (ex) {
console.log("exception ", ex);
}
Run Code Online (Sandbox Code Playgroud)
尝试使用管道,以及大多数可能的解决方案。文件存在。不明白这里出了什么问题。Readstream 中有什么吗?感谢帮助。
在花了很多时间并尝试了很多可能的事情之后,我发现我得到的错误是。
TypeError: data should be a string, Buffer or Uint8Array
Run Code Online (Sandbox Code Playgroud)
在我的 formData 中,我在文件中附加了一个变量
form.append('extractArchive', false);
Run Code Online (Sandbox Code Playgroud)
这只不过是布尔值和 axios 或 formData 对此给出了错误。我改成,
form.append('extractArchive', 'false');
Run Code Online (Sandbox Code Playgroud)
那解决了我的问题。如果有人跑步是这样的问题,它可能会有所帮助。
谢谢你的帮助。