我必须发送 header 内容类型设置为“x-www-form-urlencoded”的 post 方法的数据。
这个表单数据也是嵌套对象。 例如
const formData = { name: "hello", email:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="29484b4a694e44484045074a4644" rel="noreferrer noopener nofollow">[email protected]</a>, education: { subject: "engilsh" ... } } }
最佳答案
您可以使用querystring
模块。
像这样类似 Express 的伪代码一样发布数据:
const querystring = require('querystring');
// ...
router.post(
'https://api/url',
querystring.stringify(formData),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
)
//编辑:querystring
模块不适用于嵌套对象。我的错。我也许建议将对象序列化为 JSON 字符串。
关于javascript - 使用 x-www-form-urlencoded 内容类型将嵌套对象发布为 formdata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63524369/