vishu
vishu

Reputation: 53

Add track to playlist SoundCloud API

In my Windows Phone App, I'm using the following code to add a track to playlist (i.e. a PUT request to playlists/id endpoint)

using (HttpClient httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AccessToken);
            HttpResponseMessage response = await httpClient.PutAsync(endpoint, new StringContent(data));
            response.EnsureSuccessStatusCode();
        }

where "data" is JSON data the form:

{"playlist":{"tracks":["TrackId(to be added)"]}}

The above code returns "OK"(200) response but the track is NOT added to the playlist!

What am I doing wrong?

Upvotes: 0

Views: 693

Answers (2)

vishu
vishu

Reputation: 53

The problem was that the JSON data (of the request body) was not formatted correctly. "data" must be of the form:

    {"playlist":{"tracks":[{"id":"__"}, {"id":"__"}, {"id":"__"}]}}

Here the id-value pair must be present for

  • every track already present in the playlist, as well as

  • the track that you want to add to the playlist

(Remember, this is a PUT request. So, you need to update data i.e. update the "tracks" property of the "playlist")

Upvotes: 1

cucko
cucko

Reputation: 1436

I use Put to replace track ids in set. here is sample code

for (String s : trackIds)
    nameValuePairs.add(new BasicNameValuePair("playlist[tracks][][id]", s.trim()));

String url = "https://api.soundcloud.com/playlists/" + setId + ".json";
httpPut(url, nameValuePairs);

Upvotes: 1

Related Questions