node.js - 使用nodejs fetch和form-data时获取报告invalid_arg_type
问题描述
谁能告诉我我在这里做错了什么?我正在使用:node v14.17.3 / node-fetch / form-data 4.0.0 我的代码如下所示:
let
finalUrl = _transactionUrl.upload.url.replace('<transactionID>', _transactionID),
fd = new FormData(),
rc = 0;
fd.append('file', fs.createReadStream(p_fileName), { filename: p_fileName, contentType: 'application/octet-stream', mimeType: 'application/octet-stream' });
fd.append('id', _transactionID);
fd.append('url', '');
// (...)
const
headers = Object.assign({ Authorization: _authBase64 }, fd.getHeaders()),
fetchOptions = {
method: _transactionUrl.upload.method, // POST in this case
headers: headers,
body: fd,
}; console.dir(fetchOptions);
log(_dashLine, `fetching now: [${finalUrl}]`);
let
rsp = await fetch(finalUrl, fetchOptions);
rspText = await rsp.text(); log(rspText);
fetchOptions 对象对我来说看起来不错...
{
method: 'POST',
headers: {
Authorization: 'Basic ZHVtYXNkZW5pcy10ZXN0OkRvY2Fwb3N0ZTIwMjEh',
'content-type': 'multipart/form-data; boundary=--------------------------361340234590893974776762'
},
body: FormData {
_overheadLength: 1335,
_valueLength: 38,
_valuesToMeasure: [ [ReadStream] ],
writable: false,
readable: true,
dataSize: 0,
maxDataSize: 2097152,
pauseStreams: true,
_released: false,
_streams: [
'----------------------------361340234590893974776762\r\n' +
'Content-Disposition: form-data; name="file"; filename="contratdemo.pdf"\r\n' +
'Content-Type: application/octet-stream\r\n' +
'\r\n',
[DelayedStream],
[Function: bound ],
_currentStream: null,
_insideLoop: false,
_pendingNext: false,
_boundary: '--------------------------361340234590893974776762'
}
}
---------------- fetching now: [https://FQDN masked]/api/v2/transactions/2c96ad2c7ae1cbbe017ae9400f530a8b/document?requestReference=]
`
但 fetch 抛出此错误:
` _http_outgoing.js:722 throw new ERR_INVALID_ARG_TYPE('第一个参数', ^
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer or Uint8Array. Received type boolean (false)
at write_ (_http_outgoing.js:722:11)
at ClientRequest.write (_http_outgoing.js:687:15)
at FormData.ondata (internal/streams/legacy.js:20:31)
at FormData.emit (events.js:375:28)
at FormData.CombinedStream.write (C:\Users\ddumas\Documents\node\node_modules\combined-stream\lib\combined_stream.js:138:8)
at FormData.CombinedStream._pipeNext (C:\Users\ddumas\Documents\node\node_modules\combined-stream\lib\combined_stream.js:126:8)
at FormData.CombinedStream._realGetNext (C:\Users\ddumas\Documents\node\node_modules\combined-stream\lib\combined_stream.js:99:10)
at FormData.CombinedStream._getNext (C:\Users\ddumas\Documents\node\node_modules\combined-stream\lib\combined_stream.js:82:12)
at DelayedStream.emit (events.js:387:35)
at DelayedStream._handleEmit (C:\Users\ddumas\Documents\node\node_modules\delayed-stream\lib\delayed_stream.js:82:15) {
code: 'ERR_INVALID_ARG_TYPE'
}
有人看到我在这方面做错了吗?非常感谢帮助,
否认
解决方案
推荐阅读
- dart - 比较对象时 == 运算符在飞镖中究竟是如何工作的?
- amazon-web-services - 防止 AWS Lambda 限制异步调用
- python - Spyder 4.2.3 不显示函数变量
- export - Magento 2:import,export和importexport有什么区别
- java - 如何忽略正在测试的方法中的方法调用?
- python - 为什么我的数据表格我的word表格不保存?
- php - PDO 准备好的语句:获取 NULL 而不是预期的结果
- php - extension_dir 在 CLI 和 phpinfo() 中有不同的值;
- html - 在动画结尾隐藏文本
- c - 在 mosquitto auth 插件中删除安全连接