简体   繁体   English

使用 x-www-form-urlencoded content-type 将嵌套的 object 作为 formdata 发布

[英]post nested object as formdata using x-www-form-urlencoded content-type

I have to send the data of post method where headers content-type is set to "x-www-form-urlencoded".我必须发送标头内容类型设置为“x-www-form-urlencoded”的 post 方法的数据。

Also this form-data is nested object.此表单数据也是嵌套的 object。 eg例如

const formData = { name: "hello", email:abc@gmail.com, education: { subject: "engilsh"... } } }

You can use the querystring module.您可以使用querystring模块。

Post the data like this Express-like pseudocode:像这样发布类似 Express 的伪代码的数据:

const querystring = require('querystring');

// ...

router.post(
  'https://api/url',
  querystring.stringify(formData),
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
)

// EDIT: The querystring module does not work with nested objects. // 编辑: querystring模块不适用于嵌套对象。 My bad.我的错。 I'd perhaps suggest serializing the object into a JSON string.我可能会建议将 object 序列化为 JSON 字符串。

I assume that the problem you are experiencing is that the data received is shown as education: [object Object] .我假设您遇到的问题是收到的数据显示为education: [object Object]

The easiest way to resolve this problem is to change the header from x-www-form-urlencoded to application/json .解决此问题的最简单方法是将 header 从x-www-form-urlencoded更改为application/json That way the object with the key education won't be serialised into [object Object]这样带有密钥education的 object 就不会被序列化为[object Object]

Another way to resolve this (but hacky) is to serialise the data client side:解决此问题的另一种方法(但很麻烦)是序列化数据客户端:

const formData = { name: "hello", email:abc@gmail.com, education: { subject: "engilsh" ... } } }
const formDataSerial = { raw: JSON.stringify(formData) }
// Send to server

And at the server, do another step of unpacking:并在服务器上,进行另一步解包:

const dataSerial = formData.raw
const data = JSON.parse(dataSerial)
// Yay!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过“Content-Type”发送 JSON object:“application/x-www-form-urlencoded - Unable to send JSON object over “Content-Type”: "application/x-www-form-urlencoded “axios.defaults.headers.common['Content-Type'] = 'application/json'” 但 axios.post 仍然是“application/x-www-form-urlencoded” - "axios.defaults.headers.common['Content-Type'] = 'application/json'" but axios.post still is "application/x-www-form-urlencoded" 如何使用在Content-Type标头中同时具有“ application / x-www-form-urlencoded”和“ charset = UTF-8”的Javascript发布HTML表单 - How to post a HTML form using Javascript that has both “application/x-www-form-urlencoded” and “charset=UTF-8” in the Content-Type header 使用字符串作为请求正文时,为什么 Axios 发送我的 POST 请求时使用 Content-Type application/x-www-form-urlencoded? - Why does Axios send my POST request with Content-Type application/x-www-form-urlencoded when using a string as the request body? 使用 apollo-datasource-rest 库将 Content-Type 标头设置为 application/x-www-form-urlencoded - Setting Content-Type header to application/x-www-form-urlencoded using apollo-datasource-rest library file_get_contents():假设应用程序/x-www-form-urlencoded 未指定内容类型 - file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded "ajax、setRequestHeader()、Content-Type、application\/x-www-form-urlencoded 和 charset" - ajax, setRequestHeader(), Content-Type, application/x-www-form-urlencoded and charset 如何使发布请求具有bas ic auth和内容类型x-www-form-urlencoded - how to make post request baswith ic auth and content type x-www-form-urlencoded 如何在+呼叫后内容类型中转义+作为application / x-www-form-urlencoded - How to escape + in post call content type as application/x-www-form-urlencoded 当我的dataType:“ JSON”时,为什么我的Ajax请求发送“内容类型:application / x-www-form-urlencoded”? - Why is my Ajax request sending “Content-Type: application/x-www-form-urlencoded” when I have dataType: “JSON”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM