如何以 form-data 格式使用 Thunder 发送一个数组?

huangapple go评论59阅读模式
英文:

How to send an array with Thunder in form-data format?

问题

使用Thunder发送数组形式的表单数据

是否有办法使用Thunder以表单数据的格式发送一个数组?我需要将一个URL数组发送到字段"redirectUrls"以测试我的端点,我尝试将它作为数组"redirectUrls[0]"发送,但没有成功,有解决方案吗?

如何以 form-data 格式使用 Thunder 发送一个数组?

英文:

Sending an Array on form using thunder

Is there a way to send an array in the form-data format using Thunder?
i need to send an array of urls on the field "redirectUrls" to test my endpoint i tried to send it as an array "redirectUrls[0]" but it's did't work any solutions ?
如何以 form-data 格式使用 Thunder 发送一个数组?

答案1

得分: 1

只需添加具有相同字段名称的多个表单字段。以下是一个可工作的示例:

import express from 'express';
import multer from 'multer';

const app = express();

const upload = multer();

app.post('/', upload.array('files'), (req, res) => {
  console.log('body: ', req.body)
  console.log('files:', req.files)
  res.sendStatus(200)
})

app.listen(3000, () => console.log('Server is listening on http://localhost:3000'))

服务器日志:

body:  [Object: null prototype] { redirectUrls: [ 'a', 'b' ] }
files: [
  {
    fieldname: 'files',
    originalname: 'avatar1.jpg',
    encoding: '7bit',
    mimetype: 'image/jpeg',
    buffer: <Buffer ff d8 ff e1 00 18 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 00 00 00 00 00 00 00 00 ff ec 00 11 44 75 63 6b 79 00 01 00 04 00 00 00 3c 00 00 ff e1 03 ... 91369 more bytes>,
    size: 91419
  },
  {
    fieldname: 'files',
    originalname: 'avatar2.jpg',
    encoding: '7bit',
    mimetype: 'image/jpeg',
    buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 43 00 02 01 01 01 01 01 02 01 01 01 02 02 02 02 02 04 03 02 02 02 02 05 04 04 03 ... 35909 more bytes>,
    size: 35959
  }
]

Thunder Client v2.7.3

如何以 form-data 格式使用 Thunder 发送一个数组?

英文:

Just add multiple form fields with the same field name. Here is a working example:

import express from &#39;express&#39;;
import multer from &#39;multer&#39;;

const app = express();

const upload = multer();

app.post(&#39;/&#39;, upload.array(&#39;files&#39;), (req, res) =&gt; {
  console.log(&#39;body: &#39;, req.body)
  console.log(&#39;files:&#39;, req.files)
  res.sendStatus(200)
})

app.listen(3000, () =&gt; console.log(&#39;Server is listening on http://localhost:3000&#39;))

如何以 form-data 格式使用 Thunder 发送一个数组?

Server logs:

body:  [Object: null prototype] { redirectUrls: [ &#39;a&#39;, &#39;b&#39; ] }
files: [
  {
    fieldname: &#39;files&#39;,
    originalname: &#39;avatar1.jpg&#39;,
    encoding: &#39;7bit&#39;,
    mimetype: &#39;image/jpeg&#39;,
    buffer: &lt;Buffer ff d8 ff e1 00 18 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 00 00 00 00 00 00 00 00 ff ec 00 11 44 75 63 6b 79 00 01 00 04 00 00 00 3c 00 00 ff e1 03 ... 91369 more bytes&gt;,
    size: 91419
  },
  {
    fieldname: &#39;files&#39;,
    originalname: &#39;avatar2.jpg&#39;,
    encoding: &#39;7bit&#39;,
    mimetype: &#39;image/jpeg&#39;,
    buffer: &lt;Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 43 00 02 01 01 01 01 01 02 01 01 01 02 02 02 02 02 04 03 02 02 02 02 05 04 04 03 ... 35909 more bytes&gt;,
    size: 35959
  }
]

Thunder Client v2.7.3

huangapple
  • 本文由 发表于 2023年5月28日 02:46:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定