Skip to content Skip to sidebar Skip to footer

Require.js POST Request To Spotify Web Api Returning "Error Parsing Json"

According to Spotify Web API Create Playlist, once authorization is successful, a POST with the access_token and a few other parameters should create a new playlist for the user.

Solution 1:

Instead of using data, use body:

    var request = require('request');
    var authOptions1 = {
        url: 'https://api.spotify.com/v1/users/' + username + '/playlists',
        body: JSON.stringify({
            'name': name,
            'public': false
        }),
        dataType:'json',
        headers: {
            'Authorization': 'Bearer ' + access_token,
            'Content-Type': 'application/json',
        }
    };

    request.post(authOptions1, function(error, response, body) {
        console.log(body);
    });

that should make it.


Solution 2:

According to https://github.com/mikeal/request#requestoptions-callback

var authOptions1 = {
    url: 'https://api.spotify.com/v1/users/' + username + '/playlists',
    form: { // data = form
        'name': name,
        'public': false
    },
    json: true, // dataType: json = json: true
    headers: {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type': 'application/json',
    }
};

request.post(authOptions1, function(error, response, body) {
    console.log(body);
});

Post a Comment for "Require.js POST Request To Spotify Web Api Returning "Error Parsing Json""