Last.fm wrong api method

I am trying to write a python script to fulfill a request to Last.fm, but I keep getting the wrong method error.

I don't need links to pre-written python libraries last.fm, I'm trying to do this as a "verify what I know" 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")

lastfm.request("POST","/2.0/?",params,header)

response = lastfm.getresponse()
print response.read()
+2
source share
2 answers

You are missing the Content-type for your request: "application / x-www-form-urlencoded" . It 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()
+2
source

The Last.fm method artist.getSimilar API does not require POST; this can be done using GET.

Only API methods that modify data require a POST method.

0
source

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


All Articles