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!

CX Day is Coming!

Customer Experience (CX) Day is on October 7th!! We're so excited to bring back another day full of wonderful ...

Strengthen Your Future: A Look Back at Splunk 10 Innovations and .conf25 Highlights!

The Big One: Splunk 10 is Here!  The moment many of you have been waiting for has arrived! We are thrilled to ...

Now Offering the AI Assistant Usage Dashboard in Cloud Monitoring Console

Today, we’re excited to announce the release of a brand new AI assistant usage dashboard in Cloud Monitoring ...