2
votes

Here is the code that I am using:

    await axios.post(arg.url,
        {
            headers: {
                "Content-Type": "application/json",
                Authorization: `Bearer ${SPYauthToken}`
            },
            params: {name: "playlist"},

        }
    )

I have already previously provided access to all the necessary scopes and gotten a corresponding authentication token, yet when I try to create a playlist with the code I provided, I get an error saying "Request failed with status code 401".

Nor does it work when I tried creating a playlist in the Spotify's own API site here: https://developer.spotify.com/console/post-playlists/ After clicking "try it" using their own example, nothing happens and no playlist is created for me.

Am I doing something royally wrong, or is it something that Spotify has to sort out?

Thanks!

3
you're missing quotes on Authorization header, anyway did you tried if curl works?Kurohige
Hi, it did work using curl. So it's definitely something on my end. Regardless adding quotes didn't fix the issue.Jon
I fixed it using fetch. For some reason Axios refuses to work with POST requestsJon
please have a look to my answer maybe it solves the problemKurohige

3 Answers

2
votes

Didn't try it but you can do following

    // request data object
    const data = {
        name: "playlist"
    };

    // set the headers
    const config = {
       headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${SPYauthToken}`
        }
    };

await axios.post(arg.url, data, config);
// maybe you need to stringify data so
await axios.post(arg.url, JSON.stringify(data), config);
0
votes
    await axios.post(arg.url,
        {
            headers: {
                "Content-Type": "application/json",
                "Authorization": `Bearer ${SPYauthToken}`
            },
            params: {name: "playlist"},

        }
    )

You didn't used Quotes around Authorization

0
votes

I fixed it using fetch:

    const data = { name: 'playlist' };

    fetch(arg.url, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          "Authorization": `Bearer ${SPYauthToken}`
        },
        body: JSON.stringify(data),
      })
      .then((response) => response.json())

Works flawlessly. Axios doesn't seem to work using POST, at least not as expected, so I'll probably stop using it.

Thanks to everybody who answered.