Splunk Dev

Not able to connect to splunk using javascript sdk

kavyatim
Path Finder

I installed node.js ,copied xmljson to splunk app.
Done with everything as per the document.
The client side program is as follows:




How to start a Splunk session

<script type="text/javascript" charset="utf-8">
function displayApps() {
    var http = new splunkjs.ProxyHttp("/proxy");

    // Create a Service instance and log in 
    var service = new splunkjs.Service(http, {
        username:"admin",
        password:"kav$tim1",
        scheme:"http",
        host:"localhost",
        port:"8089",
        version:"5.0"
    });

   // Verify we logged in


 // First, we log in

service.login(function(err, success) {
// We check for both errors in the connection as well
// as if the login itself failed.
if (err || !success) {
console.log("Error in logging in");
done(err || "Login failed");
return;
}

    // Print installed apps to the console to verify login
    service.apps().fetch(function(err, apps) {
        if (err) {
       console.log("There was an error retrieving the list of applications:", err);
        done(err);
        return;
        }

        var myapps = "";
        var appsList = apps.list();
        console.log("Applications:");
        for(var i = 0; i < appsList.length; i++) {
            myapps += "App " + i + ": " + appsList[i].name + "<br/>"
        } 

        document.getElementById("applist").innerHTML=myapps;
    });

}
</script> 






Tags (1)

jitendra0101
Engager

Please help. Every time I am getting error message as "There was an error retrieving the list of applications".

0 Karma

guilhem
Contributor

I have made another question as there is not enough room in the comment. See
http://splunk-base.splunk.com/answers/84184/javascript-sdk-internal-server-error

0 Karma

Neeraj_Luthra
Splunk Employee
Splunk Employee

@guilhem - What error are you running into? Assuming that you are using node v0.8.x.

0 Karma

guilhem
Contributor

Did you solve this problem? I have exactly the same error as you describe and would be interested in a solution.

By the way you need to use node v0.8.x (and not the latest release) as otherwise it will not work. see http://splunk-base.splunk.com/answers/79378/splunk-502-javascript-sdk-server-error-500-examples-brow...

0 Karma

Neeraj_Luthra
Splunk Employee
Splunk Employee

There are a few issues with what you are doing:

  1. If you are on Splunk 5, you do not need xml2json, unless of course you are trying to run the unit tests.
  2. service.login function call is missing a couple of closing parenthesis.
  3. scheme should be https, unless you have changed the default Splunk installation.
  4. You need to serve this page from a web server and in that web server you will need to set up the proxy. This last steps is taken care of if you run the examples through node, i.e. node sdkdo examples from the command line. This is also documented at JavaScript SDK Browser Examples.
0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...