node.js - NodeJS 中的 Axios 发布请求
问题描述
我在 POSTMAN 中有一个 API 调用,我正在尝试使用 Axios 在 nodeJS 项目中复制它,但结果与 POSTMAN 不同。
在我的 body 元素内部:模型和值属性和 Authorization 是 Bearer 类型。
我得到一个数组的响应结果。
现在,我尝试使用 axios 做同样的事情,但出现错误:
代码
axios.defaults.baseURL = 'http://XXXXXXXXXXXXXXX:8069/api';
axios({
method: 'POST',
url: '/create/res.users',
data: {
models: 'res.users',
values: "{ 'login': 'john@gmail.com', 'name':'john', 'email':'john@gmail.com', 'password': '123123123' }"
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + accessToken
},
})
.then(function (response) {
console.log("Register", response);
res.status(200).send({
message: response.data
});
})
.catch(function (error) {
console.log("Error", error.response.data);
res.status(error.response.status).send({
message: error.response.data
});
});
错误
{
"message": {
"name": "odoo.exceptions.RedirectWarning",
"message": "You cannot create a new user from here.\n To create new user please go to configuration panel.\n74\nGo to the configuration panel",
"arguments": [
"You cannot create a new user from here.\n To create new user please go to configuration panel.",
74,
"Go to the configuration panel"
],
"exception_type": "error",
"code": 500,
"description": "Restful API Error"
}
}
解决方案
默认情况下,axios 将 JavaScript 对象序列化为 JSON。要改为以 application/x-www-form-urlencoded 格式发送数据,本文档可能会帮助您:
https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format
推荐阅读
- node.js - 如何在下一个 js 会话中存储大数据或使用会话文件存储?
- android - 从二维码获取结果后自动发送短信到手机号码
- maven - swagger-maven-plugin 没有生成@ApiResponse 方案
- reactjs - 如何从同一个组件调度多个动作?
- android - 如何在片段内使用地图
- powerapps - 用于打开特定表单的功能区按钮
- excel - 根据下拉列表 [Excel] 从按钮单击将文本插入活动单元格
- java - 区块链投票java程序的输入有循环问题
- linux - 为动态日期编写脚本以查找访问服务器的无效用户
- excel - intCounter,如何从 Excel 中的特定单元格开始?