首页 > 解决方案 > 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但真的不知道我做错了什么。谢谢你帮助我!

标签: node.jsapifetch

解决方案


我没有使用此特定 API 的经验,但我可以看到您在标头中发送Content-Type了值application/xml,但请求正文是 JSON 格式的。尝试发送application/json该标题。标Content-Type头在 HTTP 中用于指定正文 mime 类型。更多信息在:Content-Type HTTP 标头 - MDN

我还看到这是示例中的确切代码,但请注意它们有 2 个示例请求和 2 个示例结果,一个在 XML 中,一个在 JSON 中。


推荐阅读