gpt4 book ai didi

Python删除请求rest api(on gae)

转载 作者:太空狗 更新时间:2023-10-30 01:21:55 24 4
gpt4 key购买 nike

查找了一段时间后,找到了如下的调用需要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)

返回:

{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

返回:

HTTP 400 Bad Request

我有 JSON 工作的其他功能,所以我猜问题出在 DELETE 方法上,但我无法让它工作。

除此之外,网络应用程序在谷歌应用引擎上运行,所以我无法安装数据包,所以我想保留在预安装的库中。
任何人都有在 GAE 上执行删除请求的好方法吗? (我需要同时发送数据和标题)

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

最佳答案

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

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

The DELETE method requests that the origin server remove theassociation between the target resource and its currentfunctionality. In effect, this method is similar to the rm commandin UNIX: it expresses a deletion operation on the URI mapping of theorigin server rather than an expectation that the previouslyassociated 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 existingimplementations to reject the request.

Google App Engine 就是其中一种情况,它不允许 DELETE 请求有主体。

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

Empty JSON body.

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

我用来引用的帖子是this one .

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

关于Python删除请求rest api(on gae),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28166623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com