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!

Observability Unlocked: Kubernetes Monitoring with Splunk Observability Cloud

 Ready to master Kubernetes and cloud monitoring like the pros? Join Splunk’s Growth Engineering team for an ...

Update Your SOAR Apps for Python 3.13: What Community Developers Need to Know

To Community SOAR App Developers - we're reaching out with an important update regarding Python 3.9's ...

October Community Champions: A Shoutout to Our Contributors!

As October comes to a close, we want to take a moment to celebrate the people who make the Splunk Community ...