javascript - 帖子正文中缺少必需的参数 To
问题描述
我不确定标题或正文是否配置不正确。关于是否更改标题或正文配置错误的任何想法?
const axios = require('axios');
const url = '/my_url';
const auth = {
username: username,
password: password
};
const requestbody = {
To: 'phone',
From: 'phone 2'
};
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
const config = {
auth: auth,
headers: headers
}
try {
const response = await axios.post(url, {data: requestbody}, config);
console.log(response);
} catch (error) {
console.error(error);
}
错误如:
message: 'Missing required parameter To in the post body'
解决方案
您需要将参数字符串化,然后直接传递
const querystring = require('query-string');
const query = querystring.stringify({
To: 'phone',
From: 'phone 2'
});
let options = {
headers: {
'Authorization': AUTH_HEADER,
'Content-Type': 'application/x-www-form-urlencoded'
}
};
let axios_res = await post(url, query , options);
推荐阅读
- javascript - 基于正则表达式的字符串到数组
- windows - 在 Windows 中创建新的“执行”函数/程序(而不是命令提示符)
- amazon-web-services - 在 EC2 执行之前记录 API 网关事件
- java - Apache Beam 从事件流创建时间序列
- c# - 如何在彼此之间滑动面板c#?
- java - 在 Java 中检查外部 JSON 文件的密钥
- azure - Azure 门户中的 logger.Log 语句在哪里?
- python - 如何使用 ast 分析文件 .py
- python - TypeError:“NoneType”和“int”的实例之间不支持“>”
- java - 使用 JGit 将子模块添加到非空路径