如何使用抓取对多部分表单数据进行开机自检?

2022-08-30 04:15:02

我正在获取如下网址:

fetch(url, {
  mode: 'no-cors',
  method: method || null,
  headers: {
    'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
    'Content-Type': 'multipart/form-data'
  },
  body: JSON.stringify(data) || null,
}).then(function(response) {
  console.log(response.status)
  console.log("response");
  console.log(response)
})

我的API期望数据是的,所以我正在使用这种类型的...但它给了我一个状态代码为400的响应。multipart/form-datacontent-type

我的代码出了什么问题?


答案 1

您将 设置为 ,但随后在正文数据上使用,这将返回 .您的内容类型不匹配。Content-Typemultipart/form-dataJSON.stringifyapplication/json

您需要将数据编码为 而不是 。通常在上传文件时使用,并且比(这是HTML表单的默认值)稍微复杂一些。multipart/form-datajsonmultipart/form-dataapplication/x-www-form-urlencoded

的规范可以在 RFC 1867 中找到。multipart/form-data

有关如何通过javascript提交此类数据的指南,请参阅此处

基本思想是使用 FormData 对象(在 IE < 10 中不受支持):

async function sendData(url, data) {
  const formData  = new FormData();

  for(const name in data) {
    formData.append(name, data[name]);
  }

  const response = await fetch(url, {
    method: 'POST',
    body: formData
  });

  // ...
}

根据本文,请确保不要设置标头。浏览器将为您设置它,包括参数。Content-Typeboundary


答案 2

我最近与IPFS合作并解决了这个问题。IPFS上传文件的 curl 示例如下所示:

curl -i -H "Content-Type: multipart/form-data; boundary=CUSTOM" -d $'--CUSTOM\r\nContent-Type: multipart/octet-stream\r\nContent-Disposition: file; filename="test"\r\n\r\nHello World!\n--CUSTOM--' "http://localhost:5001/api/v0/add"

基本思想是每个部分(按字符串拆分)都有自己的标头(例如,在第二部分中)。FormData 对象为您管理所有这些,因此这是实现我们目标的更好方法。boundary--Content-Type

这转化为获取API,如下所示:

const formData = new FormData()
formData.append('blob', new Blob(['Hello World!\n']), 'test')

fetch('http://localhost:5001/api/v0/add', {
  method: 'POST',
  body: formData
})
.then(r => r.json())
.then(data => {
  console.log(data)
})