After searching for a while, I found the following solutions for an api call that requires the Delete method.
First try: (httplib library)
url = '/v1/users/'+ spotify_user_id +'/playlists/'+ playlist_id +'/tracks'
data = json.dumps({"tracks": [{ "uri" : track_uri }]})
headers = {
'Authorization' : 'Bearer ' + access_token,
'Content-Type' : 'application/json'
}
conn = httplib.HTTPSConnection('api.spotify.com')
conn.request('DELETE', url , data, headers)
resp = conn.getresponse()
content = resp.read()
return json.loads(content)
This returns:
{u'error': {u'status': 400, u'message': u'Empty JSON body.'}}
Second try:(urllib2 library)
url = 'https://api.spotify.com/v1/users/'+ spotify_user_id +'/playlists/'+ playlist_id +'/tracks'
data = json.dumps({"tracks": [{ "uri" : track_uri }]})
headers = {
'Authorization' : 'Bearer ' + access_token,
'Content-Type' : 'application/json'
}
opener = urllib2.build_opener(urllib2.HTTPHandler)
req = urllib2.Request(url, data, headers)
req.get_method = lambda: 'DELETE'
try:
response = opener.open(req).read()
return response
except urllib2.HTTPError as e:
return e
This returns:
HTTP 400 Bad Request
I have other functions where the JSON is working, so I guess the problem is with the DELETE method but I can't get it working.
Besides this, the webapp is running on google app engine so i can't install packets so I would like to remain in the pre-installed librarys.
Anyone has a good way to do a Delete request on GAE? (I need to send both data and headers)
The API is spotify: developer.spotify.com/web-api/ and I'm trying to delete a track from a playlist.
After a lot of research, I found out it is impossible.
As stated at RFC 7231 (thanks to @lukasa for pointing out that RFC 2616 is obsolete), regarding the DELETE method :
The DELETE method requests that the origin server remove the association between the target resource and its current functionality. In effect, this method is similar to the rm command in UNIX: it expresses a deletion operation on the URI mapping of the origin server rather than an expectation that the previously associated information be deleted.
That said, deleting a track shouldn't need the track_uri to be passed in the body, but it should be in the URI.
Also, in the RFC:
A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.
Google App Engine is one of this cases, not allowing a DELETE request to have a body.
That said the response from Spotify makes all the sense:
Empty JSON body.
The best way to URL fetch in google apps is probably their API for it and I'm using it know for the rest of the requests.(For the ones who are using GAE and struggling between httplib, urllib2 and others).
The post I used for reference was this one.
I've asked Spotify if there is an alternative and the response from them was to Removed my Comment on Disqus!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With