<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Logging into Splunk SDK gives 404 error in Dashboards &amp; Visualizations</title>
    <link>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87876#M4807</link>
    <description>&lt;P&gt;If I understand your question correctly, it is because you need to add a /proxy&lt;BR /&gt;
endpoint on your Express server. Let me try and explain first what we do in the&lt;BR /&gt;
SDK examples, and then this should make a bit more sense.&lt;/P&gt;

&lt;P&gt;In the SDK, when you run &lt;CODE&gt;sdkdo examples&lt;/CODE&gt; or &lt;CODE&gt;sdkdo runserver&lt;/CODE&gt;, it will start&lt;BR /&gt;
up a small webserver. You can see the code for this server here:&lt;/P&gt;

&lt;P&gt;&lt;A href="https://github.com/splunk/splunk-sdk-javascript/blob/master/bin/cli.js#L137"&gt;https://github.com/splunk/splunk-sdk-javascript/blob/master/bin/cli.js#L137&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;As you can see, this server is super simple. It has two rules:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;P&gt;If the URI starts with "/proxy", we will do some special handling, which&lt;BR /&gt;
you can see here:&lt;BR /&gt;
&lt;A href="https://github.com/splunk/splunk-sdk-javascript/blob/master/bin/cli.js#L80"&gt;https://github.com/splunk/splunk-sdk-javascript/blob/master/bin/cli.js#L80&lt;/A&gt;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;If it is anything else, we assume we need to serve a static file from the&lt;BR /&gt;
filesystem.&lt;/P&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;The interesting case is (1), which you can see from the code. All it does is&lt;BR /&gt;
look at the request that came in, and then "re-make" that request to the Splunk&lt;BR /&gt;
server. This is because of the Single Origin Policy, since we can't talk &lt;BR /&gt;
cross-domain.&lt;/P&gt;

&lt;P&gt;For Express, you can do something very similar. You can use the &lt;CODE&gt;app.all&lt;/CODE&gt; function,&lt;BR /&gt;
something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;app.all('/proxy/*', function(req, res) {
    var error = {d: { __messages: [{ type: "ERROR", text: "Proxy Error", code: "PROXY"}] }};

    var writeError = function() {
        res.writeHead(500, {});
        res.write(JSON.stringify(error));
        res.end();
    };

    try {      
        var body = "";
        req.on('data', function(data) {
            body += data.toString("utf-8");
        });

        req.on('end', function() {
            var destination = req.headers["X-ProxyDestination".toLowerCase()];

            var options = {
                url: destination,
                method: req.method,
                headers: {
                    "Content-Length": req.headers["content-length"],
                    "Content-Type": req.headers["content-type"],
                    "Authorization": req.headers["authorization"]
                },
                followAllRedirects: true,
                body: body,
                jar: false
            };

            try {
                request(options, function(err, response, data) {
                    try {
                        var statusCode = (response ? response.statusCode : 500) || 500;
                        var headers = (response ? response.headers : {}) || {};

                        res.writeHead(statusCode, headers);
                        res.write(data || JSON.stringify(err));
                        res.end();
                    }
                    catch (ex) {
                        writeError();
                    }
                });
            }
            catch (ex) {
                writeError();
            }

        });
    }
    catch (ex) {
        writeError();
    }
});
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I basically just copied the code from the example server. It probably needs a bit&lt;BR /&gt;
of tweaking to confirm to the Express API, but should be pretty straightforward.&lt;/P&gt;

&lt;P&gt;Hopefully this makes sense - let me know if I should clarify anything!&lt;/P&gt;</description>
    <pubDate>Mon, 08 Jul 2013 23:43:48 GMT</pubDate>
    <dc:creator>ineeman</dc:creator>
    <dc:date>2013-07-08T23:43:48Z</dc:date>
    <item>
      <title>Logging into Splunk SDK gives 404 error</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87873#M4804</link>
      <description>&lt;P&gt;I'm trying the basic examples using the Javascript Splunk SDK using Node 0.8.0.&lt;/P&gt;

&lt;P&gt;Trying to login to Splunk through the API gives me a 404 error.&lt;/P&gt;

&lt;P&gt;My Javascript code - &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;username = "admin";
password = "changeme";
scheme   = "https";
host     = "localhost";
port     = "8089";
version  = "5.0";

Async = splunkjs.Async;
utils = splunkjs.Utils;

var tag = null;

done = callback = function() {
    $(tag).remove();
};

var http = new splunkjs.ProxyHttp("/proxy");
var service = new splunkjs.Service(http, {
    username: username,
    password: password,
    scheme: scheme,
    host: host,
    port: port,
    version: version
});

Async.chain([
        // First, we log in
        function(done) {
            service.login(done);
        },
        // Retrieve the apps
        function(success, done) {
            if (!success) {
                done("Error logging in");
            }

            service.apps().fetch(done);
        },
        // Print them out
        function(apps, done) {     
            var appsList = apps.list();       
            console.log("Applications:");
            for(var i = 0; i &amp;lt; appsList.length; i++) {
                var app = appsList[i];
                console.log("  App " + i + ": " + app.name);
            } 
            done();
        }
    ],
    function(err) {
        callback(err);        
    }
);
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is in my own code. The same script works when run from the SDK examples. What am I doing wrong?&lt;/P&gt;

&lt;P&gt;EDIT - My backend is running in Node.JS so I'm confused as to whether I have to change some config files or not.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jul 2013 18:45:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87873#M4804</guid>
      <dc:creator>narabhut</dc:creator>
      <dc:date>2013-07-08T18:45:49Z</dc:date>
    </item>
    <item>
      <title>Re: Logging into Splunk SDK gives 404 error</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87874#M4805</link>
      <description>&lt;P&gt;There doesn't seem to be anything wrong with the code. I tried it (on apache) and it worked just fine.&lt;/P&gt;

&lt;P&gt;Due to &lt;A href="http://en.wikipedia.org/wiki/Same_origin_policy"&gt;Same Origin Policy&lt;/A&gt; issues, you do need to set up a proxy for node to handle the requests to Splunk's REST APIs, i.e. on port 8089. For example, on apache, I have modified its httpd.conf file as follows:&lt;PRE&gt;&lt;CODE&gt;SSLProxyEngine On&lt;BR /&gt;
ProxyPass   /proxy/             &lt;A href="https://localhost:8089/"&gt;https://localhost:8089/&lt;/A&gt;&lt;BR /&gt;
&lt;/CODE&gt;&lt;/PRE&gt;&lt;/P&gt;

&lt;P&gt;This works in the SDK examples for you because the examples are shipped with special handling for the "/proxy" requests.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jul 2013 21:08:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87874#M4805</guid>
      <dc:creator>Neeraj_Luthra</dc:creator>
      <dc:date>2013-07-08T21:08:29Z</dc:date>
    </item>
    <item>
      <title>Re: Logging into Splunk SDK gives 404 error</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87875#M4806</link>
      <description>&lt;P&gt;I'm not using Apache though, I'm using Node.JS (along with Express) for my backend. (I just checked on my dev machine and I don't have Apache installed). Is there something I can do in Node/Express to do the same?&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jul 2013 21:23:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87875#M4806</guid>
      <dc:creator>narabhut</dc:creator>
      <dc:date>2013-07-08T21:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: Logging into Splunk SDK gives 404 error</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87876#M4807</link>
      <description>&lt;P&gt;If I understand your question correctly, it is because you need to add a /proxy&lt;BR /&gt;
endpoint on your Express server. Let me try and explain first what we do in the&lt;BR /&gt;
SDK examples, and then this should make a bit more sense.&lt;/P&gt;

&lt;P&gt;In the SDK, when you run &lt;CODE&gt;sdkdo examples&lt;/CODE&gt; or &lt;CODE&gt;sdkdo runserver&lt;/CODE&gt;, it will start&lt;BR /&gt;
up a small webserver. You can see the code for this server here:&lt;/P&gt;

&lt;P&gt;&lt;A href="https://github.com/splunk/splunk-sdk-javascript/blob/master/bin/cli.js#L137"&gt;https://github.com/splunk/splunk-sdk-javascript/blob/master/bin/cli.js#L137&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;As you can see, this server is super simple. It has two rules:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;P&gt;If the URI starts with "/proxy", we will do some special handling, which&lt;BR /&gt;
you can see here:&lt;BR /&gt;
&lt;A href="https://github.com/splunk/splunk-sdk-javascript/blob/master/bin/cli.js#L80"&gt;https://github.com/splunk/splunk-sdk-javascript/blob/master/bin/cli.js#L80&lt;/A&gt;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;If it is anything else, we assume we need to serve a static file from the&lt;BR /&gt;
filesystem.&lt;/P&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;The interesting case is (1), which you can see from the code. All it does is&lt;BR /&gt;
look at the request that came in, and then "re-make" that request to the Splunk&lt;BR /&gt;
server. This is because of the Single Origin Policy, since we can't talk &lt;BR /&gt;
cross-domain.&lt;/P&gt;

&lt;P&gt;For Express, you can do something very similar. You can use the &lt;CODE&gt;app.all&lt;/CODE&gt; function,&lt;BR /&gt;
something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;app.all('/proxy/*', function(req, res) {
    var error = {d: { __messages: [{ type: "ERROR", text: "Proxy Error", code: "PROXY"}] }};

    var writeError = function() {
        res.writeHead(500, {});
        res.write(JSON.stringify(error));
        res.end();
    };

    try {      
        var body = "";
        req.on('data', function(data) {
            body += data.toString("utf-8");
        });

        req.on('end', function() {
            var destination = req.headers["X-ProxyDestination".toLowerCase()];

            var options = {
                url: destination,
                method: req.method,
                headers: {
                    "Content-Length": req.headers["content-length"],
                    "Content-Type": req.headers["content-type"],
                    "Authorization": req.headers["authorization"]
                },
                followAllRedirects: true,
                body: body,
                jar: false
            };

            try {
                request(options, function(err, response, data) {
                    try {
                        var statusCode = (response ? response.statusCode : 500) || 500;
                        var headers = (response ? response.headers : {}) || {};

                        res.writeHead(statusCode, headers);
                        res.write(data || JSON.stringify(err));
                        res.end();
                    }
                    catch (ex) {
                        writeError();
                    }
                });
            }
            catch (ex) {
                writeError();
            }

        });
    }
    catch (ex) {
        writeError();
    }
});
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I basically just copied the code from the example server. It probably needs a bit&lt;BR /&gt;
of tweaking to confirm to the Express API, but should be pretty straightforward.&lt;/P&gt;

&lt;P&gt;Hopefully this makes sense - let me know if I should clarify anything!&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jul 2013 23:43:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87876#M4807</guid>
      <dc:creator>ineeman</dc:creator>
      <dc:date>2013-07-08T23:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: Logging into Splunk SDK gives 404 error</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87877#M4808</link>
      <description>&lt;P&gt;If this code is running from server-side, the HttpProxy is not required. you can use a pure splunkjs.Http object as the first parameter to Service creation.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2013 06:45:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87877#M4808</guid>
      <dc:creator>yitzarad</dc:creator>
      <dc:date>2013-07-09T06:45:01Z</dc:date>
    </item>
    <item>
      <title>Re: Logging into Splunk SDK gives 404 error</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87878#M4809</link>
      <description>&lt;P&gt;This is absolutely correct, I did not consider that they may want to just do a server-side connection &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;Also, if you just want a default server-side connection, you don't need to pass anything to Service, it will do the correct thing by default.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2013 17:05:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Logging-into-Splunk-SDK-gives-404-error/m-p/87878#M4809</guid>
      <dc:creator>ineeman</dc:creator>
      <dc:date>2013-07-09T17:05:26Z</dc:date>
    </item>
  </channel>
</rss>

