0

I just started learning bash and I wanted to implement the last.fm API using the curl command. I tried executing following command:

curl http://ws.audioscrobbler.com/2.0/?method=tag.gettopalbums&tag=disco&api_key=(my API KEY)&format=json

but for some reason it gives me the error code 6 "invalid parameters". If I open the link normally it works just fine.

I tried

curl -v http://ws.audioscrobbler.com/2.0/?method=tag.gettopalbums&tag=disco&api_key=(my API KEY)&format=json

and saw

HTTP/1.1 400 Bad Request

But I don't really know what that means.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Odin
  • 1
  • Have you URL encoded your data? Have you read the Last.fm documentation? Your curl appears to be going to an insecured web address (http) is this correct? – Martin Mar 21 '23 at 23:33
  • 5
    Quote your URL with quotation marks, i.e. `curl "http://ws.audioscrobbler.com/..."`, otherwise everything after `&` will be executed as a new command in different thread. You got error code 6 because the tag value is not passed over as part of URL, while it is a required parameter. – Taylor G. Mar 22 '23 at 07:14

1 Answers1

0

As stated by Taylor G. you need to enclose the URL in quotation marks as for example the & in that URL would be interpreted as a new command.

Executing the following

curl -v "http://ws.audioscrobbler.com/2.0/?method=tag.gettopalbums&tag=disco&api_key=api_key&format=json"

will give me this output:

> GET http://ws.audioscrobbler.com/2.0/?method=tag.gettopalbums&tag=disco&api_key=api_key&format=json HTTP/1.1
> Host: ws.audioscrobbler.com
> User-Agent: curl/7.81.0
> Accept: */*
> Proxy-Connection: Keep-Alive
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: openresty/1.13.6.2
< Date: Fri, 30 Jun 2023 09:01:33 GMT
< Content-Type: application/json; charset=UTF-8
< Content-Length: 34591
< Access-Control-Request-Headers: Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport, *
< Access-Control-Allow-Methods: POST, GET, OPTIONS
< Access-Control-Allow-Origin: *
< Access-Control-Max-Age: 86400
< Via: 1.1 google
< Proxy-Connection: Keep-Alive
< Connection: Keep-Alive
< 

{
    "albums": {
        "album": [
            {
                "name": "Dynamite",
                "mbid": "",
                "url": "https://www.last.fm/music/BTS",
                "artist": {
                    "name": "BTS",
                    "mbid": "0d79fe8e-ba27-4859-bb8c-2f255f346853",
                    "url": "https://www.last.fm/music/BTS"
                },
                "image": [
                    {
                        "#text": "https://lastfm.freetls.fastly.net/i/u/34s/41b15d8a0ad6a81323b598bfb19cede9.png",
                        "size": "small"
                    },
                    {
                        "#text": "https://lastfm.freetls.fastly.net/i/u/64s/41b15d8a0ad6a81323b598bfb19cede9.png",
                        "size": "medium"
                    },
                    {
                        "#text": "https://lastfm.freetls.fastly.net/i/u/174s/41b15d8a0ad6a81323b598bfb19cede9.png",
                        "size": "large"
[..]

Also you wrote:

I saw HTTP/1.1 400 Bad Request but I don't really know what that means.

In such cases it might be worth looking at the response body as it may contain more information. Here is the response body for the curl request without quotation marks:

<?xml version="1.0" encoding="UTF-8"?>
<lfm status="failed">
  <error code="6">Invalid parameters - Your request is missing a required parameter</error>
</lfm>

APIs often have these error messages so you don't need to rely only on the HTTP status code.

jmizv
  • 1,172
  • 2
  • 11
  • 28