“创建播放列表”,Spotify Web API返回201,但没有播放列表对象

时间:2019-06-20 16:31:54

标签: javascript http async-await spotify fetch-api

我正在尝试使用Spotify Web API创建一个Web应用程序,其中一部分包括创建播放列表,然后在其中添加一些曲目。

根据API reference,对请求的响应应返回一个播放列表对象,其中包含播放列表中的详细信息,更重要的是,对我来说,新播放列表的API端点,以便我可以立即使用它来添加轨道。

我得到的响应是201,我可以看到在Spotify中创建的新播放列表,但是响应中没有正文,也没有端点与创建的播放列表进行交互。

我的请求是否格式错误,或者我在响应中缺少某些内容?

// 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

1 个答案:

答案 0 :(得分:1)

为了将来参考,我的问题是我在使用主体之前忘记应用response.json(),因此我正在打印Response流而不是Promise的实际分辨率。

相关问题