Polar accesslink API POST users not working

Viewed 335

I'm currently trying to implement Polar accesslink API on an app, but when trying to POST a new user I still get this error:

    url: 'https://www.polaraccesslink.com/v3/users/',
    status: 400,
    statusText: 'Bad Request',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0

I already have the authorization token, which I know expires every 10min, and I'm using the service through a function that takes the token and the userID as parameters. This is my code:

 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);
    });
  })
}

I'm implementing it the same way as it is specified in https://www.polar.com/accesslink-api/?javascript--nodejs#users but really don't know what might I be doing wrong. Thanks for helping me!.

2 Answers

I don't have experience with this specific API but i can see you send in the header Content-Type the value application/xml but the request body is JSON formatted. Try send application/json in that header. The Content-Type header is used in HTTP to specify the body mime type. more info in: Content-Type HTTP Header - MDN

I also see this is the exact code in the sample but notice they have 2 sample requests and 2 sample results, one in XML and one in JSON each.

Any ideea why this POST gives 'invalid_Request'?

curl --location --request POST 'https://polarremote.com/v2/oauth2/token?grant_type=authorization_code&code=1f9edc0c5e60a0bab4fd3f1f00571a58' --header 'Authorization: Basic ... DA4OS05ZDc2LTJlNTQwZjFkZTc5ZA==' --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json'
Related