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 mand in the link
curl -X POST ""
-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: '/' + 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);
});
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 mand in the link
curl -X POST "https://api.spotify./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./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);
});
Share
Improve this question
edited Jul 6, 2014 at 4:50
woodstok
asked Jul 5, 2014 at 19:30
woodstokwoodstok
2,8043 gold badges36 silver badges53 bronze badges
2
-
Remove
JSON.stringify()
; it should make thedata
property into JSON for you. – Jared Farrish Commented Jul 5, 2014 at 19:54 - Tried this. It does not work. Same error – woodstok Commented Jul 6, 2014 at 4:49
2 Answers
Reset to default 8Instead of using data
, use body
:
var request = require('request');
var authOptions1 = {
url: 'https://api.spotify./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.
According to https://github./mikeal/request#requestoptions-callback
var authOptions1 = {
url: 'https://api.spotify./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);
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744817510a4595415.html
评论列表(0条)