Search code examples
reactjsfetchjsxspotify

Spotify WebPlayer API - Change Player Request


I've been working on a Spotify player using React, and so far I've been able to get the player to properly connect to the API and receive a device_id.

My next step is change the player to the newly "made" one, but I keep on receiving 400: Required field device_ids missing, even though I have it included in the PUT request:

const requestOptions = {
    method: 'PUT',
    headers: {
        'Authorization' : 'Bearer ' + spotifyAccessToken 
    }, 
    data: {
        "device_ids": [device_id],
        "play": 'true'
    }
};

// Change player
fetch('https://api.spotify.com/v1/me/player', requestOptions)

Am I missing something/formatting it incorrectly? I've tried changing the data to body and I instead get a 400: Malformed JSON.

Thanks in advance!


Solution

  • I was able to get this working using the following format:

    const requestOptions = {
        method: 'PUT',
        body: JSON.stringify({
            device_ids: [southORadioDevId],
            play: 'true'
        }),
        headers: {
            Authorization: `Bearer ${spotifyAccessToken}`
        },
    };
    
    // Change player
    fetch('https://api.spotify.com/v1/me/player', requestOptions)