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!

2024 Splunk Career Impact Survey | Earn a $20 gift card for participating!

Hear ye, hear ye! The time has come again for Splunk's annual Career Impact Survey!  We need your help by ...

Optimize Cloud Monitoring

  TECH TALKS Optimize Cloud Monitoring Tuesday, August 13, 2024  |  11:00AM–12:00PM PST   Register to ...

What's New in Splunk Cloud Platform 9.2.2403?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.2.2403! Analysts can ...