Announcements

Help Wizard

Step 1

NEXT STEP

Error Code 411 even after Content-Length: 0

Error Code 411 even after Content-Length: 0

Hello,
so I have been experiencing an issue with the /pause request. I can do GET /currently-playing. But when I do /pause I first get 411. Trying to solve this issue with pauseConnection.setRequestProperty("Content-Length": "0");. Doesnt work. Then looked at this issue: Cant post the link to the issue lol. It was a issue on gh where someone had the same issue. Then I added setDoOutput(true) and getOutputStream.close(). I got the error 401 back. Thanks!. My final request (it still doesnt work -.-):

public static void togglePlayPause() {
try {
// Send request to pause playback
URL pauseUrl = new URL("https://api.spotify.com/v1/me/player/pause");
HttpURLConnection pauseConnection = (HttpURLConnection) pauseUrl.openConnection();

// Set the request method to PUT
pauseConnection.setRequestMethod("PUT");

// Set the required headers
pauseConnection.setRequestProperty("Authorization", "Bearer " + accessToken);
pauseConnection.setRequestProperty("Content-Length", "0");

// Enable the output stream, so we can have a request body, but don't write anything to it
pauseConnection.setDoOutput(true); // This indicates that the connection has a body, even if it's empty

// Since we're not sending any data, we don't need to write to the output stream
pauseConnection.getOutputStream().close(); // Close the stream immediately without writing anything

// Get the response code
int responseCode = pauseConnection.getResponseCode();
System.out.println("Response Code: " + responseCode);
} catch (IOException e) {
e.printStackTrace();
}
}
Reply
0 Replies

Suggested posts

Let's introduce ourselves!

Hey there you,   Yeah, you! 😁   Welcome - we're glad you joined the Spotify Community!   While you here, let's have a fun game and get…

ModeratorStaff / Moderator/ 4 years ago  in Social & Random