【问题标题】:Polar accesslink API POST users not workingPolar accesslink API POST 用户不工作
【发布时间】:2021-02-25 22:55:56
【问题描述】:

我目前正在尝试在应用上实现 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.js api fetch


    【解决方案1】:

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

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

    【讨论】:

    • 我知道这不是代码审查,但我看不出有任何理由在这里使用 new Promise() 作为 fetch 返回承诺。如果在 then 块之一中,您需要包装一个基于回调的函数,您可以使用仅用于该调用的 promise 构造函数和 return then 块中的新承诺。他们将链接在一起。
    猜你喜欢
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 2018-11-23
    • 2015-10-29
    • 2012-03-03
    • 2015-11-21
    相关资源
    最近更新 更多