MrJalapeno
MrJalapeno

Reputation: 1662

Reason for different API responses to request in node and chrome?

So I have a bunch of tracks from Spotify's API and I want their genres (which Spotify doesn't give) so for every track I make an API call to Last FM to get their top tags. Now this works for most tracks, I have to match the track name and artist as strings to last fm:

Here's my problem:
I do like this (pseudo:ish code):

let promises = spotifyTracks
      .map(track => rp({url: "http://lastfmapi.com/?artist="+track.artist+"?track="+track.name })
                .then(response => {
                    track.genre = response.genre;
                    return track;
                })
      );
return Promise.all(promises).then(() => console.log('done!'));

Using request promise.

Now there a few tracks that currrently baffles me. Like 10 in 600. I get a response from lastFM saying:

{ error: 6, message: 'Track not found', links: [] }

To double check I printed the url used:

"http://lastfmapi.com/?artist="+track.artist+"?track="+track.name

Inside the then-call along with the response.

Now if I copied that url from my output and pasted it right into my chrome-browsers address-bar, the API finds the track!?!??!

the actual example

http://ws.audioscrobbler.com/2.0/?method=track.gettoptags&artist=pugh+rogefeldt&track=små+lätta+moln&autocorrect=1&api_key=141bed9ffc180dd9b07ac93b7e3b56d7&format=json

When it is called in my node-code I get

{ error: 6, message: 'Track not found', links: [] }

when called in the chrome address bar I get

{"toptags": {
    "tag":
        [
            {
                "count":100,
                "name":"swedish",
                "url":"https://www.last.fm/tag/swedish"
            },
            {
                "count":100,
                "name":"singer-songwriter",
                "url":"https://www.last.fm/tag/singer-songwriter"
            }, 
            ...
        ],
        "@attr":{
            "artist":"Pugh Rogefeldt",
            "track":"Små lätta moln"
        }
    }
} 

Anyone got any idea what could be the reason behind this discrepancy?

Upvotes: 0

Views: 57

Answers (1)

Ryan W
Ryan W

Reputation: 6173

Chrome address bar will encode the string into URL for you, which will make your actual example become

method=track.gettoptags&artist=pugh+rogefeldt&track=sm%C3%A5+l%C3%A4tta+moln&autocorrect=1&api_key=141bed9ffc180dd9b07ac93b7e3b56d7&format=json

You should do the same thing in your node-code with encodeURIComponent

Upvotes: 1

Related Questions