5

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.

3
  • 1
    Thank you so much for putting effort into asking your question. You've renewed my faith in SO, at least for a day. Commented Jan 27, 2015 at 9:04
  • Your second example looks odd Have you considered just trying urlfetch - it supports the DELETE method. cloud.google.com/appengine/docs/python/urlfetch/fetchfunction Commented Jan 27, 2015 at 10:14
  • I've tried to use to urlfetch and I get this response: {u'error': {u'status': 400, u'message': u'Empty JSON body.'}} Commented Jan 28, 2015 at 16:03

1 Answer 1

3

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!!

Sign up to request clarification or add additional context in comments.

3 Comments

An update on this. RFC 7231 Section 4.3.5, which supersedes your link to RFC 2616, says that "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." This is not the same as saying that a payload on DELETE is forbidden: it's entirely allowed. However, it means that using one is tricky and best avoided.
While we're here, it might be helpful to note that all of the Python HTTP libraries allow you to send a body when you're not using GAE: only on GAE do they forbid it.
Thanks @Lukasa, I've edited my answer with the new RFC. The answer remains the same because Spotify removed my comment on Disqus

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.