[GH-ISSUE #767] Getting malformed JSON error when passing playlist as a uri_context #466

Closed
opened 2026-02-27 23:22:48 +03:00 by kerem · 3 comments
Owner

Originally created by @captpanther on GitHub (Jan 4, 2022).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/767

When supply a uri_context and an offset to the start_playback method I receive an error 400 stating malformed JSON

code

scope = "playlist-modify-private playlist-read-private playlist-read-collaborative user-read-playback-state user-modify-playback-state"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
    client_id=os.environ.get('SPOTIPY_CLIENT_ID')
    ,client_secret=os.environ.get('SPOTIPY_CLIENT_SECRET')
    ,redirect_uri=os.environ.get('SPOTIPY_REDIRECT_URI')
    ,scope=scope))

def playlist_playback(sp, context_uri, offset):
    sp.start_playback(context_uri=context_uri, offset=offset)

context_uri = 'spotify:playlist:#################'
offset = 1

res = sp.devices()
print(res)

try:
    playlist_playback(sp,context_uri,offset)
except Exception as e:
    print (e)
print (sp.current_playback())

Additional Testing
What I expect is the song at the specified index of the supplied playlist to start playing, I tested the context uri on the spotify developer dashboard and it worked without issue

Error in console

HTTP Error for PUT to https://api.spotify.com/v1/me/player/play withTest\SpotipyTest> Params: {} returned 400 due to Malformed json
http status: 400, code:-1 - https://api.spotify.com/v1/me/player/play:
Malformed json, reason: None

My Environment:

  • OS: Windows 10
  • Python version 3.10
  • Spotipy version 2.19
  • IDE: VSCode

Not sure what I might be doing wrong, but any help is greatly appreciated

Originally created by @captpanther on GitHub (Jan 4, 2022). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/767 When supply a uri_context and an offset to the start_playback method I receive an error 400 stating malformed JSON **code** ```py scope = "playlist-modify-private playlist-read-private playlist-read-collaborative user-read-playback-state user-modify-playback-state" sp = spotipy.Spotify(auth_manager=SpotifyOAuth( client_id=os.environ.get('SPOTIPY_CLIENT_ID') ,client_secret=os.environ.get('SPOTIPY_CLIENT_SECRET') ,redirect_uri=os.environ.get('SPOTIPY_REDIRECT_URI') ,scope=scope)) def playlist_playback(sp, context_uri, offset): sp.start_playback(context_uri=context_uri, offset=offset) context_uri = 'spotify:playlist:#################' offset = 1 res = sp.devices() print(res) try: playlist_playback(sp,context_uri,offset) except Exception as e: print (e) print (sp.current_playback()) ``` **Additional Testing** What I expect is the song at the specified index of the supplied playlist to start playing, I tested the context uri on the [spotify developer dashboard](https://developer.spotify.com/console/put-play/) and it worked without issue **Error in console** > HTTP Error for PUT to https://api.spotify.com/v1/me/player/play withTest\SpotipyTest> Params: {} returned 400 due to Malformed json > http status: 400, code:-1 - https://api.spotify.com/v1/me/player/play: > Malformed json, reason: None **My Environment:** - OS: Windows 10 - Python version 3.10 - Spotipy version 2.19 - IDE: VSCode **Not sure what I might be doing wrong, but any help is greatly appreciated**
kerem 2026-02-27 23:22:48 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@captpanther commented on GitHub (Jan 4, 2022):

Ok, I was able to get this working, not sure if this a bug or just a change in the API request format, however the parameter for offset must be nested, this is what I have come up with that seem to address the problem if you need to supply an offset:

scope = "playlist-modify-private playlist-read-private playlist-read-collaborative user-read-playback-state user-modify-playback-state"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
    client_id=os.environ.get('SPOTIPY_CLIENT_ID')
    ,client_secret=os.environ.get('SPOTIPY_CLIENT_SECRET')
    ,redirect_uri=os.environ.get('SPOTIPY_REDIRECT_URI')
    ,scope=scope))

def playlist_playback(sp,device,context_uri,offset,ms):
    sp.start_playback(device_id=device,context_uri=context_uri, offset=offset, position_ms=ms)

context_uri= "spotify:playlist:<playlist_id>"
offset = {"position":1}  # Just wrap the offset parameter in its own dictionary, it will nest appriately in the start_playback method
res =sp.devices()
first_device = res['devices'][0].get('id')

if first_device:
    try:
        playlist_playback(sp,first_device,context_uri,offset,ms=0)
    except Exception as e:
        print (e)
<!-- gh-comment-id:1005205013 --> @captpanther commented on GitHub (Jan 4, 2022): Ok, I was able to get this working, not sure if this a bug or just a change in the API request format, however the parameter for offset must be nested, this is what I have come up with that seem to address the problem if you need to supply an offset: ```py scope = "playlist-modify-private playlist-read-private playlist-read-collaborative user-read-playback-state user-modify-playback-state" sp = spotipy.Spotify(auth_manager=SpotifyOAuth( client_id=os.environ.get('SPOTIPY_CLIENT_ID') ,client_secret=os.environ.get('SPOTIPY_CLIENT_SECRET') ,redirect_uri=os.environ.get('SPOTIPY_REDIRECT_URI') ,scope=scope)) def playlist_playback(sp,device,context_uri,offset,ms): sp.start_playback(device_id=device,context_uri=context_uri, offset=offset, position_ms=ms) context_uri= "spotify:playlist:<playlist_id>" offset = {"position":1} # Just wrap the offset parameter in its own dictionary, it will nest appriately in the start_playback method res =sp.devices() first_device = res['devices'][0].get('id') if first_device: try: playlist_playback(sp,first_device,context_uri,offset,ms=0) except Exception as e: print (e) ```
Author
Owner

@Peter-Schorn commented on GitHub (Jan 4, 2022):

The documentation for start_playback specifically says:

Provide offset as {"position": <int>} or {"uri": "<track uri>"} to start playback at a particular offset.

<!-- gh-comment-id:1005225914 --> @Peter-Schorn commented on GitHub (Jan 4, 2022): The documentation for `start_playback` specifically says: > Provide `offset` as {"position": \<int>} or {"uri": "\<track uri>"} to start playback at a particular offset.
Author
Owner

@captpanther commented on GitHub (Jan 4, 2022):

I must have overlooked that, my brain is on info overload, but thanks for pointing that out

<!-- gh-comment-id:1005232705 --> @captpanther commented on GitHub (Jan 4, 2022): I must have overlooked that, my brain is on info overload, but thanks for pointing that out
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#466
No description provided.