Dashboards & Visualizations

Why am I getting "Error: Http instance required when creating a Context within a browser." when running my JavaScript code?

syx093
Communicator

I am getting this error when running my JavaScript code:

Error: Http instance required when creating a Context within a browser. 

I am running the code in the browser. I know I am supposed to have an http implementation, but how do I go about creating that?

kleymenova
Engager

So if http proxy is required why do you provide the code example having no any mention of it like this one?
http://dev.splunk.com/view/javascript-sdk/SP-CAAAEDD

Another issue - even with proxy it doesn't work. In my case the local splunk server is located on different domain, and there is no any description or clue how to provide the right address to it. Because all the data used in object as a second parameter in service.login is not used.

saiteja
New Member

Hi,

In order to do login request or any other request you have to create http instance.

var http = new splunkjs.ProxyHttp("/proxy");

As per your context this should solve your problem.

0 Karma

woodcock
Esteemed Legend

You are missing your code.

0 Karma

syx093
Communicator

I fixed it but now I am running across a new error. "uncaught exception: No session key available"

The following is my code:

<script type="text/javascript" src="/home/sxiong/Documents/splunk_java_script/splunk-sdk-javascript-master/client/splunk.js"></script>
<script type="text/javascript" src="/home/sxiong/Documents/splunk_java_script/splunk-sdk-javascript-master/examples/browser/resources/jquery.min.js
"></script>

<script type="text/javascript" charset="utf-8">
    var http = new splunkjs.ProxyHttp("/proxy");
    var service = new splunkjs.Service(http,{username: "admin", password: "12334", scheme: "https", host: "megaman", port: "8000", version: "6.2"});
    service.login(function(err, success) {
        if (err) {
            throw err;
        }

        console.log("Login was successful: " + success);
        service.jobs().fetch(function(err, jobs) {
            var jobList = jobs.list();
            for(var i = 0; i < jobList.length; i++) {
                console.log("Job " + i + ": " + jobList[i].sid);
            }
        });
    });

</script>

any help is appreciated.

0 Karma

syx093
Communicator

I manage to fix the problem but now I have a new problem. I am trying to use the code from splunk's client side JavaScript and I am getting the error: ReferenceError: displayApps is not defined

the url is:
http://dev.splunk.com/view/javascript-sdk/SP-CAAAEC9

here is my code:

<html>
  <head>
    <meta charset="utf-8">
    <title>How to start a Splunk session</title>
    <script type="text/javascript" src="/home/sxiong/Documents/splunk_java_script/splunk-sdk-javascript-master/examples/browser/resources/jquery.min.js"></script>
    <script type="text/javascript" src="/home/sxiong/Documents/splunk_java_script/splunk-sdk-javascript-master/client/splunk.js"></script>
    <script type="text/javascript" src="/home/sxiong/Documents/splunk_java_script/splunk-sdk-javascript-master/examples/browser/resources/json2.js">

    <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: "changeme",
            scheme: "https",
            host: "megaman",
            port:"8000",
            version:"6.2.4"
        });


        // Verify we logged in

        // Print installed apps to the console to verify login
        service.apps().fetch(function(err, apps) {
            if (err) {
                alert("Error listing apps");
                return;
            }

            var myapps = "";
            var appsList = apps.list();
            for(var i = 0; i < appsList.length; i++) {
                myapps += "App " + i + ": " + appsList[i].name + "  

"
}

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

    }
    </script> 
</head>
<body>
    <button type="button" onclick="displayApps()">Log in and list apps</button>
    <p id="applist"></p>
</body>
</html>

there is some formatting error screwing up the for loop. Just use the like as a reference.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...

Design, Compete, Win: Submit Your Best Splunk Dashboards for a .conf26 Pass

Hello Splunkers,  We’re excited to kick off a Splunk Dashboard contest! We know that dashboards are a primary ...

May 2026 Splunk Expert Sessions: Security & Observability

Level Up Your Operations: May 2026 Splunk Expert Sessions Whether you are refining your security posture or ...