javascript - 在 Node 中使用 axios 发送 POST 数据的正确方法是什么?
问题描述
我正在尝试使用Node.js 中的axios连接到Monzo。Monzo 使用 OAuth2 发送请求,我正在努力获取访问令牌。这是文档中的请求:
http --form POST "https://api.monzo.com/oauth2/token" \
"grant_type=authorization_code" \
"client_id=$client_id" \
"client_secret=$client_secret" \
"redirect_uri=$redirect_uri" \
"code=$authorization_code"
所以有了这个数据变量:
const data = {
grant_type: 'authorization_code',
client_id: oauth_12345,
client_secret: hunter2,
redirect_uri: https://localhost:3000/,
code: 1234567890
};
我尝试了以下请求:
1.
var response = await axios.post("https://api.monzo.com/oauth2/token", data);
2.
const qs = require('qs'); //The qs library is used to turn objects into URL parameter strings
var response = await axios.post("https://api.monzo.com/oauth2/token", qs.stringify(data));
3.
const qs = require('qs');
const config = {headers: 'Content-Type' : 'application/x-www-form-urlencoded'};
var response = await axios.post("https://api.monzo.com/oauth2/token", qs.stringify(data), config);
4.
var response = await axios({
"method": "post",
"url": "https://api.monzo.com/oauth2/token",
"data": data
);
5.
const qs = require('qs');
var response = await axios({
"method": "post",
"url": "https://api.monzo.com/oauth2/token",
"data": qs.stringify(data),
"headers" : {
"Content-Type": 'application/x-www-form-urlencoded'
}
);
我不断收到同样的错误:400 Bad Request Your request has missing arguments or is malformed.
,除了据我所知,我没有遗漏的参数,也没有格式错误。通过在线阅读,错误表明 axios 没有通过data
POST 请求正确发送,但我会感谢任何人提供的任何见解。
由于我无法控制要发送到的 API,有没有办法验证 axios 发送了什么?
我的任何请求都应该有效吗?
提前致谢
解决方案
推荐阅读
- android-studio - 默认工具栏和状态栏未显示在 android studio 3.3 中的 xml 预览中
- r - R Shiny 没有上传
- java - Swagger codegen 不生成带有 JAXB 注释的模型类
- java - 如何设置 Mockito 模拟以对多个不同的方法调用使用相同的答案
- python - 无法将 Celery 任务日志推送到 Google Cloud(stackdriver)
- debugging - OTS 解析错误:CFF2:未对齐的表
- vbscript - 在文本文件顶部插入行无法使用 VBScript
- qt - 请求 http URL 时,QT 报告“QNetworkReplyHttpImplPrivate::_q_startOperation 被多次调用”
- c - 使用 MPI_Isend 时出现分段错误
- asp.net-core - 为什么 ASP.NET Core 不强制 Startup 类型实现接口