1

I'm learning node and I'm trying to use the Spotify API to search and return an artist. The page loads and everything, but then when I try to search, I get this error

undefined:1
<html> 
^
SyntaxError: Unexpected token <
    at Object.parse (native)
    at IncomingMessage.<anonymous> (/Users/edwinzhang/Node_Courses/spotify-recommend/server.js:23:25)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)

After some digging, I found the reason I was getting this error was due to this:

var searchReq = http.get(options, function(response) {
    response.on('data', function(chunk) {
        item += chunk;
        console.log(item);
    });
    response.on('end', function() {
        console.log('end');
        console.log(item);
        item = JSON.parse(item);
        emitter.emit('end', item);
    });
    response.on('error', function() {
        emitter.emit('error');
    });
});

In response.on('data', function(chunk) ..., chunk was returning

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
 <hr><center>nginx</center>
</body>
</html>

The path for the request is (supposedly) api.spotify.com/v1/search?q=sam&limit=1&type=artist. Does anyone know why I am getting this error? Thanks!

user1998511
  • 455
  • 1
  • 8
  • 21
  • 1
    You don't specify, are you using HTTPS or HTTP? The URL you provide is valid. You can paste it into a browser and verify you do receive results. However, if you provide a non-SSL URL spotify is 301 responding and forwarding the request to the HTTPS equivalent. This may or may not be relevant to what you're attempting as from everything I've seen spotify is handling the URL re-write for you. Still something, to check. – Khepri Aug 20 '15 at 01:06
  • @Khepri ah i see. i'm using HTTP. It seems the url automatically redirects to HTTPS. Would that be the reason for my error? – user1998511 Aug 20 '15 at 01:08
  • Just an observation. It may or may not be relevant. I'd try changing the URL in your API call to HTTPS to eliminate the 301 redirect and see if your results change. Probably isn't an issue, but...process of elimination. – Khepri Aug 20 '15 at 01:11
  • It's relevant, please use HTTPS. (I work at Spotify) – Michael Thelin Aug 20 '15 at 07:00

1 Answers1

1

So I decided to sit down and fire up IntelliJ to take a look at your issue. I'm not sure of the why (Node isn't my main dev platfrom), but yes, this is a http / https issue. Someone more versed in node will have to explain.

If I require http and try to make the call as such, I do receive the response you are seeing. HTTP

If I switch to including https, it works as expected.

HTTPS

Unfortunately, I don't know the "why" of it. It's been some time since I've worked in node so I'm a tad rusty, but at least there is a workable solution.

FOLLOW UP:

It seems by default http will not follow redirects. You have to use the request module instead and there is an option you can set to specify to follow redirects.

How do you follow an HTTP Redirect in Node.js?

Community
  • 1
  • 1
Khepri
  • 9,547
  • 5
  • 45
  • 61