1

I am trying to create a playlist using the Spotify API and am receiving the following error:

{
  "error" : {
    "status" : 400,
    "message" : "Error parsing JSON."
  }
}

I am following the example on this page that says to use the following curl request

curl -X POST "https://api.spotify.com/v1/users/thelinmichael/playlists" -H "Authorization: Bearer {your access token}" -H "Content-Type: application/json" --data "{\"name\":\"A New Playlist\", \"public\":false}"   

My code looks like this

$.ajax({
   url: 'https://api.spotify.com/v1/users/{id}/playlists',
   method: "POST",
   data: {
     name: "test"
   },
   headers: {
     'Authorization': 'Bearer ' + access_token,
     'Content-Type': 'application/json'
   },
   success: function(response) {
     console.log(response);
   }
 });

Can anyone advise what I am doing wrong?

  • check your token again the ajax is correct – HudsonPH Nov 02 '16 at 14:25
  • @HudsonPH i don't believe there is an issue with the token, as I am able to make other requests, it is just the POST request that is catching me –  Nov 02 '16 at 15:26

1 Answers1

0

The reason was that I was not posting valid son. By using JSON.stringify I was able to solve this issue.

JSON.stringify({name: "test", public: false})

Complete code looks like this:

$.ajax({
   url: 'https://api.spotify.com/v1/users/{id}/playlists',
   method: "POST",
   data: {
     name: JSON.stringify({name: "test", public: false})
   },
   headers: {
     'Authorization': 'Bearer ' + access_token,
     'Content-Type': 'application/json'
   },
   success: function(response) {
     console.log(response);
   }
 });