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!

Prove Your Splunk Prowess at .conf25—No Prereqs Required!

Your Next Big Security Credential: No Prerequisites Needed We know you’ve got the skills, and now, earning the ...

Splunk Observability Cloud's AI Assistant in Action Series: Observability as Code

This is the sixth post in the Splunk Observability Cloud’s AI Assistant in Action series that digs into how to ...

Splunk Answers Content Calendar, July Edition I

Hello Community! Welcome to another month of Community Content Calendar series! For the month of July, we will ...