Announcements

Help Wizard

Step 1

NEXT STEP

400 cannot remove song from playlist

400 cannot remove song from playlist

Plan

Premium

Country

USA

Device

Samsung Galaxy S9

Operating System

Android

 

My Question or Issue

I'm totally unable to use the web API to remove a song from a playlist.  Pretty sure I have the request set up correctly, I've checked everything multiple times.

 

It returns a 400 every time without any other details.  Can someone please let me know what I may be doing wrong?  I've tried it both with and without the quotes in the JSON object keys and values.

 

  Thanks in advance!  Here's the code:

 

private void removeCurrentlyPlayingSongFromPlaylist(String playlistId) {

JSONObject tracksToDelete = new JSONObject();
JSONArray tracksArray = new JSONArray();

JSONObject thisTrackObject = new JSONObject();

try {
thisTrackObject.put("\"uri\"", "\"" + currentTrack.uri + "\"");
tracksArray.put(thisTrackObject);
tracksToDelete.put("\"tracks\"", tracksArray);

String requestUrl = getPlaylistTracksAPICallUrl(playlistId);

volleyQueue.makeRequest(requestUrl, VolleyQueue.REQUEST_TYPE_DELETE, response -> {
Toast.makeText(MainActivity.this, "Song successfully removed from playlist!", Toast.LENGTH_SHORT).show();
}, error -> {
int z = 0;
}, tracksToDelete);

} catch (JSONException e) {

}
}

public void makeRequest(String requestUrl, int requestType, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener, JSONObject jsonData) {
JsonObjectRequest request = null;
switch (requestType) {
case REQUEST_TYPE_GET:
request = getVolleyGETRequest(requestUrl, listener, errorListener);
break;
case REQUEST_TYPE_DELETE:
request = getVolleyDELETERequest(requestUrl, listener, errorListener, jsonData);
break;
case REQUEST_TYPE_POST:
request = getVolleyPOSTRequest(requestUrl, listener, errorListener);
break;
}
if (request != null) {
queue.add(request);
}
}


private JsonObjectRequest getVolleyDELETERequest(String url, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener, JSONObject jsonData) {
return new JsonObjectRequest(Request.Method.DELETE, url, jsonData, listener, errorListener) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", "Bearer " + preferences.getString(StandardStrings.PreferencesKeys.SPOTIFY_ACCESS_TOKEN, "null"));
params.put("Content-Type", "application/json");
return params;
}
};
}



 

Reply
1 Reply

What you have all looks sensible from the point of view of someone who doesn't write Java. I am a bit surprised you have to do so much quoting... could you be over-doing it and making it look to the API like you are not providing the required "tracks" object? Maybe try logging your request before sending it?

Despite what the documentation says, there are (or used to be) two different request formats when removing tracks. You can see the other one if you fill sample data in the developer console.

 

The 400 error is when:

 

> Attempting to use several different ways to remove items returns 400 Bad Request."

 

Which makes zero sense considering the current documentation but makes a bit more sense if you know there are actually multiple ways to use this endpoint. We currently use the undocumented way in our application and I can verify it works (but we did previously use the documented way also, I think...). I would say the undocumented way is the only sensible way because a playlist may contain multiple copies of the same track and you want to be able to specify exactly which of the occurrences should be removed.

 

To be honest I don't know why you need to specify track URIs in addition to track indexes. It seems pointless, especially if you have provided the snapshot ID. This, along with the bad documentation, is exactly the kind of thing we could have submitted Github issues about in the past, shame someone decided to remove that feedback channel. It was probably the same person who redesigned the documentation and now makes me to scroll down a massive long page of methods in a random order. And the same person that enforced the aggressive logout timeout on this forum. And added the recaptchas... /rant.

Suggested posts