通过axios提交表单数据失败,dataSize=0

np8igboo  于 2022-11-05  发布在  iOS
关注(0)|答案(1)|浏览(290)

我使用form-data npm包从我的服务器(node/express)向另一个服务器发送一个图像列表,我使用axios发送表单数据。
我获取图像列表的方法是从一个blob格式的URL中获取它们,并将它们附加到表单数据中,如下所示:formData.append('pictures[]', picture) .
问题是在某些情况下,当我试图将表单数据提交到另一个服务器时,我会收到下面的错误。问题是这并不是在所有情况下都会发生,这就是所有这些问题的奇怪之处。

{
  "message": "Request failed with status code 500",
  "name": "Error",
  "stack": "Error: Request failed with status code 500n    at createError (/app/node_modules/axios/lib/core/createError.js:16:15)n    at settle (/app/node_modules/axios/lib/core/settle.js:17:12)n    at IncomingMessage.handleStreamEnd (/app/node_modules/axios/lib/adapters/http.js:293:11)n    at IncomingMessage.emit (node:events:525:35)n    at IncomingMessage.emit (node:domain:489:12)n    at endReadableNT (node:internal/streams/readable:1359:12)n    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)",
  "config": {
    "transitional": {
      "silentJSONParsing": true,
      "forcedJSONParsing": true,
      "clarifyTimeoutError": false
    },
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "timeout": 0,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": null,
    "maxBodyLength": null,
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "multipart/form-data; boundary=--------------------------505025809634644491355142",
      "User-Agent": "axios/0.24.0"
    },
    "method": "post",
    "url": "<some url here>",
    "data": {
      "_overheadLength": 375,
      "_valueLength": 136604,
      "_valuesToMeasure": [],
      "writable": false,
      "readable": true,
      "dataSize": 0,
      "maxDataSize": 2097152,
      "pauseStreams": true,
      "_released": true,
      "_streams": [],
      "_currentStream": null,
      "_insideLoop": false,
      "_pendingNext": false,
      "_boundary": "--------------------------505025809634644491355142",
      "_events": {},
      "_eventsCount": 1
    }
  },
  "status": 500
}

正如你从错误中看到的,dataSize是0,这完全出乎意料。
有没有人以前遇到过这种情况,或者可以告诉我哪些资源可以帮助我调试这种情况?
以下是将所有图像添加到表单数据的代码:

const form = new FormData()
let promises = []
for (let i = 0; i < pictureUrls.length; i++) {
  promises.push(axios.get(pictures[i], {responseType: 'blob'}));
}
Promise.all(promises).then(values => {
  for (let i = 0; i < values.length; i++) {
    form.append('pictures[]', values[i].data);
  }
})

下面是我用来发送图像的代码:

axios.post(URL, form, {auth, headers: form.getHeaders(), maxContentLength: Infinity, maxBodyLength: Infinity }
axkjgtzd

axkjgtzd1#

我建议您只使用内置的fetch而不是axios(需要NodeJS v18)
NodeJS内置了对获取、FormData和Blob的支持。
但它们尚不支持通过文件系统备份文件/blob 1(https://github.com/nodejs/node/issues/37340)
但是FormData实现支持第三方Blob实现。因此,我建议您安装fetch-blob

import { fileFromSync } from 'fetch-blob/from.js'

// In case you are using CommonJS
// const { fileFromSync } = await import('fetch-blob/from.js')

const file = fileFromSync('package.json')
const fd = new FormData()
const res = await fetch('https://httpbin.org/post', { 
  method: 'POST', 
  body: fd 
})
const json = await res.json()

它知道如何串行化和设置具有边界的正确请求报头。

相关问题