Python 删除请求休息 api (on gae)

Python Delete Request for a rest api (on gae)

经过一段时间的搜索,我找到了以下针对需要 Delete 方法的 api 调用的解决方案。

首先尝试:(httplib 库)

    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)

这个returns:

{u'error': {u'status': 400, u'message': u'Empty JSON body.'}}

第二次尝试:(urllib2库)

    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

这个returns:

HTTP 400 Bad Request

我有其他函数可以使用 JSON,所以我猜问题出在 DELETE 方法上,但我无法让它工作。

除此之外,Web 应用程序在 google 应用程序引擎上 运行,因此我无法安装数据包,所以我想保留在 pre-installed 库中。
任何人都有在 GAE 上执行删除请求的好方法吗? (我需要同时发送数据和 headers)

API 是 spotify: developer.spotify.com/web-api/ 我正在尝试从播放列表中删除曲目。

经过大量研究,我发现这是不可能的。

RFC 7231 (thanks to @lukasa 所述,指出 RFC 2616 已过时),关于 DELETE 方法:

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.

也就是说,删除曲目不需要在正文中传递 track_uri,但它应该在 URI 中。

此外,在 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 就是其中一种情况,不允许 DELETE 请求有主体。

也就是说 Spotify 的回复很有道理:

Empty JSON body.

URL 在 google 应用程序中获取的最佳方式可能是 their API for it,我正在使用它来处理其余的请求。(对于那些正在使用GAE 和在 httplib、urllib2 和其他之间挣扎)。

我参考的post是this one.

我问过 Spotify 是否有替代方案,他们的回答是删除了我对 Disqus 的评论!!