首页 > 解决方案 > 使用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'
}

有人看到我在这方面做错了吗?非常感谢帮助,

否认

标签: node.jsfetchform-data

解决方案


推荐阅读