简体   繁体   English

如何使用 Axios 在 application/x-www-form-urlencoded 中编码 JSON 数据?

[英]How to encode JSON data in application/x-www-form-urlencoded using Axios?

I have to make a post request to an API endpoint and it is required that the body of the request is encoded in application/x-www-form-urlencoded.我必须向 API 端点发出 post 请求,并且要求请求的正文在 application/x-www-form-urlencoded 中编码。

Here is what I am currently doing:这是我目前正在做的事情:

  // Request data
  const data = {
    grant_type: "client_credentials",
  };

  // Request configuration
  const config = {
    method: "post",
    url,
    data,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      Authorization:
        "Basic " +
        Buffer.from(clientId + ":" + clientSecret).toString("base64"),
    },
  };

  return axios(config).then(.....

As you can see, I have my data in JSON format, so how can I pass it encoded in application/x-www-form-urlencoded?如您所见,我的数据为 JSON 格式,那么如何将其编码为 application/x-www-form-urlencoded 传递?

Any ideas?有任何想法吗? Thank you.谢谢你。

This:这个:

JSON.stringify(data);

will return将返回

'data = {"grant_type": "client_credentials"}'

application/x-www-form-urlencoded means you can: application/x-www-form-urlencoded意味着您可以:

Send FormData body: axios post request to send form data发送FormData body: axios post请求发送表单数据

Or send data in the url query string: How to post query parameters with Axios?或者在url查询字符串中发送数据: How to post query parameters with Axios?

You can also combine both.您也可以将两者结合起来。

This encoding is often used when JSON encoding doesn't meet the requirements eg sending files.当 JSON 编码不符合要求(例如发送文件)时,通常使用此编码。 You could send a file in a json string but that would be a base64 encoded file as string which increases the size.您可以在 json 字符串中发送一个文件,但这将是一个 base64 编码的文件作为字符串,这会增加大小。

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

相关问题 如何使用应用程序 / x-www-form-urlencoded 发送 axios 帖子? - How to send axios post with application / x-www-form-urlencoded? JSON以下数据如何转换为application/x-www-form-urlencoded? - How to convert below JSON data to 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" 如何将 x-www-form-urlencoded 字符串转换为 JSON? - How to convert an x-www-form-urlencoded string to JSON? 如何将x-www-form-urlencoded有效负载转换为json - how to convert x-www-form-urlencoded payload to json 在JQuery上使用x-www-form-urlencoded数据 - Using x-www-form-urlencoded data on JQuery 坚持使用 reduce function 将 application/x-www-form-urlencoded 转换为 JSON - Stuck at converting application/x-www-form-urlencoded to JSON using reduce function 如何将javascript json数据转换为x-www-form-urlencoded? - How can I convert javascript json data to be x-www-form-urlencoded? 如何使用节点请求发送承载令牌和x-www-form-urlencoded数据 - How to send bearer token and x-www-form-urlencoded data using Node Request 使用 axios 发出 x-www-form-urlencoded 请求 - Making a x-www-form-urlencoded request with axios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM