Alex King
Alex King

Reputation: 29

"Create a Playlist" with Spotify Web API returning 201 but without a playlist object

I'm trying to create a web app using the Spotify Web API, part of which includes creating a playlist and then filling it with some tracks.

According to the API reference, the response to the request should return a playlist object containing details on the playlist and, more importantly for me, the API endpoint for the new playlist so that I can immediately use it to add tracks.

The response I'm getting is 201, and I can see the new playlist created in Spotify, but the response contains no body and nowhere is the endpoint to interact with the created playlist.

Is my request somehow malformed or am I missing something in the response?

// Test create playlist
  const createPlaylist = async (accessToken, userID, tracks) => {
    // Create empty playlist and retrieve endpoint
    const emptyPlaylist = await fetch(`https://api.spotify.com/v1/users/${userID}/playlists`, {
      method: 'POST',
      body: JSON.stringify({
        'name': 'Intersection Test',
        'public': false,
      }),
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + accessToken
      }
    })
    .then(async response => {
      // Add tracks to playlist
      if (tracks.length > 100) error("Playlist too large for one call");
      const fillPlaylist = await fetch(response.url, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + accessToken
        },
        body: {
          'uris': tracks
        }
      });
    });

Image of response in Chrome console here

Upvotes: 0

Views: 1041

Answers (1)

Alex King
Alex King

Reputation: 29

For future reference, my problem was that I forgot to apply response.json() before using the body, so I was printing the Response stream rather than the actual resolution of the Promise.

Upvotes: 2

Related Questions