Search code examples
javascriptnode.jssoundcloud

Making HTTP requests from server side


I have some code that is trying to get a JSON result from the Soundcloud API.

I registered an app, got the client id and such, and I'm trying to make a call like this:

var querystring = require('querystring');
var http = require('http');

var addr = 'http://api.soundcloud.com/resolve.json?url=http://soundcloud.com/matas/hobnotropic&client_id=XXXXX';

var options = {
    hostname: "api.soundcloud.com",
    path: "/resolve.json?url=http://soundcloud.com/matas/hobnotropic&client_id=XXXXXx",
    method: "GET",
    headers: {
        "Content-Type": "application/json"
    }
}

var request = http.get(options, function(response) {
    response.setEncoding('utf8');
    response.on('data', function(chunk) {

        console.log(chunk);
    });
});

This produces a result that looks like this:

{"status":"302 - Found","location":"https://api.soundcloud.com/tracks/49931.json?client_id=xxxx"}

When I use the same URL in Chrome, I get the proper JSON info. How do I properly make this call from server side script?


Solution

  • The built-in http client does not handle redirects. However request does and has many other features the built-in client does not support out of the box.