require.js POST request to spotify web api returning "Error parsing json"
javascript, json, node.js, spotify
Solution
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.
Problem
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. The example CURL command in the link ``` curl -X POST "https://api.spotify.com/v1/users/wizzler/playlists" -H "Authorization: Bearer {your access token}" -H "Content-Type: application/json" --data "{\"name\":\"A New Playlist\", \"public\":false}" ``` This working fine for me. But when i run the following code from a nodejs application, using `request` library, the response stats `Error parsing json`. What am i missing here? Update: I tried changing `data` to `form` as per request.js examples. I also tried removing the stringify call, and passed the object directly. The error still persists. ``` var request = require('request'); var authOptions1 = { url: 'https://api.spotify.com/v1/users/' + username + '/playlists', data: JSON.stringify({ 'name': name, 'public': false }), dataType:'json', headers: { 'Authorization': 'Bearer ' + access_token, 'Content-Type': 'application/json', } }; console.log(authOptions1); request.post(authOptions1, function(error, response, body) { console.log(body); }); ```