node.js - Polar accesslink API POST 用户不工作
问题描述
我目前正在尝试在应用程序上实现 Polar accesslink API,但是在尝试发布新用户时,我仍然收到此错误:
url: 'https://www.polaraccesslink.com/v3/users/',
status: 400,
statusText: 'Bad Request',
headers: Headers { [Symbol(map)]: [Object: null prototype] },
counter: 0
我已经有了授权令牌,我知道它每 10 分钟过期一次,并且我正在通过一个将令牌和用户 ID 作为参数的函数来使用该服务。这是我的代码:
postUser(memberId: string, token: string) {
const inputBody = { 'member-id': memberId };
const headers = {
'Content-Type': 'application/xml', 'Accept': 'application/json', 'Authorization': 'Bearer ' + token
};
const options = {
method: "POST",
headers: headers,
body: JSON.stringify(inputBody)
}
return new Promise<object>((resolve, reject) => {
fetch('https://www.polaraccesslink.com/v3/users', options)
.then(function(res: any) {
console.log(res)
return res.json();
}).then(function(body: any) {
console.log(body);
});
})
}
我以与中指定的方式相同的方式实现它,https://www.polar.com/accesslink-api/?javascript--nodejs#users
但真的不知道我做错了什么。谢谢你帮助我!
解决方案
我没有使用此特定 API 的经验,但我可以看到您在标头中发送Content-Type
了值application/xml
,但请求正文是 JSON 格式的。尝试发送application/json
该标题。标Content-Type
头在 HTTP 中用于指定正文 mime 类型。更多信息在:Content-Type HTTP 标头 - MDN
我还看到这是示例中的确切代码,但请注意它们有 2 个示例请求和 2 个示例结果,一个在 XML 中,一个在 JSON 中。
推荐阅读
- angular - 从地图角度获取属性
- python - 导入和独立函数
- google-apps-script - 谷歌工作表没有将行复制到正确的工作表
- node.js - React + Express google Oauth 错误 - CORS 策略:请求的资源上不存在“Access-Control-Allow-Origin”标头
- python - 在为斐波那契数列编写代码时,我面临的问题是索引错误
- reactjs - 通过单击主页按钮,我想显示一个组件,通过单击购物车按钮,我想显示另一个组件(使用布尔值)
- javascript - 如何在包含具有数组值的子对象的对象中搜索值
- flutter - 为什么使用这个简单的 initState 代码会导致 Flutter 崩溃?
- vba - InternetExplorer html attribute change
- python - 为什么不能从python中的主类导入子类