I am trying to write a python script to do a query to Last.fm, but I keep getting an invalid method error returned.
I don't want links to pre-written last.fm python libraries, I am trying to do this as a "test what I know" kind of project. Thanks in advance!
import urllib
import httplib
params = urllib.urlencode({'method' : 'artist.getsimilar',
'artist' : 'band',
'limit' : '5',
'api_key' : #API key goes here})
header = {"user-agent" : "myapp/1.0"}
lastfm = httplib.HTTPConnection("ws.audioscrobbler.com")开发者_如何学JAVA
lastfm.request("POST","/2.0/?",params,header)
response = lastfm.getresponse()
print response.read()
You lack Content-type for your request: "application/x-www-form-urlencoded". This works:
import urllib
import httplib
params = urllib.urlencode({'method' : 'artist.getsimilar',
'artist' : 'band',
'limit' : '5',
'api_key' : '#API key goes here'})
header = {"user-agent" : "myapp/1.0",
"Content-type": "application/x-www-form-urlencoded"}
lastfm = httplib.HTTPConnection("ws.audioscrobbler.com")
lastfm.request("POST","/2.0/?",params,header)
response = lastfm.getresponse()
print response.read()
the Last.fm artist.getSimilar API method does not require POST, it can be done with a GET.
Only API methods that change data require the POST method.
精彩评论