javascript - 如何使用 Axios 对 application/x-www-form-urlencoded 中的 JSON 数据进行编码?
问题描述
我必须向 API 端点发出 post 请求,并且要求请求的主体在 application/x-www-form-urlencoded 中编码。
这是我目前正在做的事情:
// 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(.....
如您所见,我的数据是 JSON 格式的,那么如何将其以 application/x-www-form-urlencoded 编码传递?
有任何想法吗?谢谢你。
解决方案
这个:
JSON.stringify(data);
将返回
'data = {"grant_type": "client_credentials"}'
推荐阅读
- reactjs - 反应选择不适用于反应传单弹出
- java - 创建数据矩阵时如何使用 pantone 代码?
- cmd - 如何将新创建的文件从多个目录复制到单个目录 uisng ssis 或 cmd?
- java - java - 如何在java中获取字符串格式时间“2019-01-25 14:34:34.123456789”的纪元以来的纳秒?
- c# - KeyValue 列表使用哪种数据结构
- symfony - 在 Syfmony PHPUnit 测试中模拟两个 ObjectRepositories
- python - 无法使用漂亮的搜素从 Python 中的网页中提取图像
- php - 将长字符串分配给变量和随后将较小的字符串附加到变量之间在性能上有什么区别吗?
- windows - Golang os.Rename(
, ) 在 Windows 中不起作用 - node.js - gyp动词`which`失败错误:未找到:python2