Python Remove Rest Request api (on gae)

After some searching, I found the following solutions for calling api, which requires the Delete method.

First attempt: (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 attempt: (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 that JSON works in, so I think the problem is with the DELETE method, but I can't get it to work.

In addition, webapp runs on the Google engine, so I can’t install the packages, so I would like to stay in the pre-installed libraries.
Who has a good way to make a delete request in GAE? (I need to send data and headers)

The API defines: developer.spotify.com/web-api/, and I'm trying to remove a track from a playlist.

+5
source share
1 answer

After many studies, I found out that this is not possible.

As indicated on RFC 7231 (thanks @lukasa for pointing out that RFC 2616 is deprecated) regarding the DELETE method:

The DELETE method requests that the source server remove the connection between the target resource and its current functionality. In essence, this method is similar to the rm command on UNIX: it expresses the delete operation on the URI of the source server, and not the expectation that previously associated information will be deleted.

However, to delete a track, track_uri does not need to be transmitted in the body, but it must be in the URI.

In addition, in the RFC:

The payload in the DELETE request message has no specific semantics; sending a payload body on a DELETE request may result in a reject request.

The Google App Engine is one such case, not allowing the DELETE request to have a body.

However, Spotify's answer makes sense:

The empty JSON body.

The best way to URL search in Google apps is probably their API for it , and I use it for the rest (for those who use GAE and struggle between httplib, urllib2 and others). A.

The message I used for the link was this one .

I asked Spotify if there is an alternative, and the answer from them was to delete my comment on Disqus!

+3
source

Source: https://habr.com/ru/post/1211982/


All Articles