All Apps and Add-ons

How to access splunk server through JAVA Script using HTTPS:

mpreddy
Communicator

Hi Splunkers,

I have a requirement to access the splunk indexer and pull data in json format using Java script and to display in the Java script UI

I played around with CURL but no luck.followed documentation.I am trying to connect with HTTPS call

http://docs.splunk.com/Documentation/Splunk/6.5.1/RESTTUT/RESTsearches

my search query:

Index=_internal|table source,sourcetype,host

these 3 fields i need to show in the UI. Any one have have working code for this?

1 Solution

vasanthmss
Motivator

Hi,

save the below code as html, replace parameters with your search string
add the basic authorization, mypostrequest.setRequestHeader("Authorization", "Basic <>");

  <html>
    <head>
    <title>Ajax POST HTTP Request to Splunk Rest</title>
    <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript"> 
function ajaxRequest(){
     var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]; //activeX versions to check for in IE
     if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
      for (var i=0; i<activexmodes.length; i++){
       try{
        return new ActiveXObject(activexmodes[i]);
       }
       catch(e){
        //suppress error
       }
      }
     }
     else if (window.XMLHttpRequest) // if Mozilla, Safari etc
      return new XMLHttpRequest();
     else
      return false;
}
    function schedule_job(){
     var mypostrequest=new ajaxRequest();
        mypostrequest.onreadystatechange=function(){
         if (mypostrequest.readyState==4){
          if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
           document.getElementById("result").innerHTML = mypostrequest.responseText;
          }
          else{
           alert("An error has occurred making the request");
          }
         }
        }  
        var parameters="search=search index=_internal|table source,sourcetype,host"
        mypostrequest.open("POST", "https://localhost:8089/services/search/jobs", true);
        mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        mypostrequest.setRequestHeader("Authorization", "Basic <>");
        mypostrequest.send(parameters);
     }
    function getJob(){
     var mypostrequest=new ajaxRequest();
        mypostrequest.onreadystatechange=function(){
         if (mypostrequest.readyState==4){
          if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
           document.getElementById("result").innerHTML = mypostrequest.responseText;
          }
          else{
           alert("An error has occurred making the request");
          }
         }
        } 
        var jobId = encodeURIComponent(document.getElementById("jobs").value);
        var parameters="search=search index=_internal|head 10|table hose, source,sourcetype"
        mypostrequest.open("GET", "https://localhost:8089/services/search/jobs/"+jobId+"/results?output_mode=json", true);
        mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        mypostrequest.setRequestHeader("Authorization", "Basic <>");
        mypostrequest.send();
     }
    </SCRIPT>
    </head>
    <body>

        <form method="get" action="">
            <table>
                <tr>
                    <td>Job ID</td>
                    <td><input type="text" id="jobs" name="jobs" size="30" />
                    </td>
                </tr>       
                <tr>
                   <input type="button" value="Schedule_Job" onClick="schedule_job()" />
                   <input type="button" value="getJob" onClick="getJob()" />
                    </td>
                </tr>
            </table>
        </form>

        <div id="result"></div>
    </body>
    </html>
V

View solution in original post

vasanthmss
Motivator

Hi,

save the below code as html, replace parameters with your search string
add the basic authorization, mypostrequest.setRequestHeader("Authorization", "Basic <>");

  <html>
    <head>
    <title>Ajax POST HTTP Request to Splunk Rest</title>
    <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript"> 
function ajaxRequest(){
     var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]; //activeX versions to check for in IE
     if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
      for (var i=0; i<activexmodes.length; i++){
       try{
        return new ActiveXObject(activexmodes[i]);
       }
       catch(e){
        //suppress error
       }
      }
     }
     else if (window.XMLHttpRequest) // if Mozilla, Safari etc
      return new XMLHttpRequest();
     else
      return false;
}
    function schedule_job(){
     var mypostrequest=new ajaxRequest();
        mypostrequest.onreadystatechange=function(){
         if (mypostrequest.readyState==4){
          if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
           document.getElementById("result").innerHTML = mypostrequest.responseText;
          }
          else{
           alert("An error has occurred making the request");
          }
         }
        }  
        var parameters="search=search index=_internal|table source,sourcetype,host"
        mypostrequest.open("POST", "https://localhost:8089/services/search/jobs", true);
        mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        mypostrequest.setRequestHeader("Authorization", "Basic <>");
        mypostrequest.send(parameters);
     }
    function getJob(){
     var mypostrequest=new ajaxRequest();
        mypostrequest.onreadystatechange=function(){
         if (mypostrequest.readyState==4){
          if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
           document.getElementById("result").innerHTML = mypostrequest.responseText;
          }
          else{
           alert("An error has occurred making the request");
          }
         }
        } 
        var jobId = encodeURIComponent(document.getElementById("jobs").value);
        var parameters="search=search index=_internal|head 10|table hose, source,sourcetype"
        mypostrequest.open("GET", "https://localhost:8089/services/search/jobs/"+jobId+"/results?output_mode=json", true);
        mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        mypostrequest.setRequestHeader("Authorization", "Basic <>");
        mypostrequest.send();
     }
    </SCRIPT>
    </head>
    <body>

        <form method="get" action="">
            <table>
                <tr>
                    <td>Job ID</td>
                    <td><input type="text" id="jobs" name="jobs" size="30" />
                    </td>
                </tr>       
                <tr>
                   <input type="button" value="Schedule_Job" onClick="schedule_job()" />
                   <input type="button" value="getJob" onClick="getJob()" />
                    </td>
                </tr>
            </table>
        </form>

        <div id="result"></div>
    </body>
    </html>
V
Get Updates on the Splunk Community!

The OpenTelemetry Certified Associate (OTCA) Exam

What’s this OTCA exam? The Linux Foundation offers the OpenTelemetry Certified Associate (OTCA) credential to ...

From Manual to Agentic: Level Up Your SOC at Cisco Live

Welcome to the Era of the Agentic SOC   Are you tired of being a manual alert responder? The security ...

Splunk Classroom Chronicles: Training Tales and Testimonials (Episode 4)

Welcome back to Splunk Classroom Chronicles, our ongoing series where we shine a light on what really happens ...