Getting Data In

How to call a Rest/Json service on HTML button click action from dashboard search

kig121
Loves-to-Learn Lots

Hi,

I have a splunk search which give back the testcase_id's.

I need a button which call a rest API request.

Rest API input should the testcase_id from splunk search. and separate the testcase_id with coma.

kig121_0-1623228534550.png

 

Labels (1)
Tags (1)
0 Karma

kig121
Loves-to-Learn Lots

Hi,@kamlesh_vaghela 

Independent from table. Somwehere in Dashboard

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@kig121 

Are expecting button in table cell or somewhere else?

KV

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@kig121 

Can you please try this Sample Dashboard?

XML:

<dashboard script="html_button.js">
  <label>HTML Button</label>
  
  <search base="main_search">
    <query>| stats delim="," values(testcase_id) as testcase_id | mvcombine testcase_id</query>
    <done>
      <set token="tkn_testcase_ids">$result.testcase_id$</set>
    </done>
  </search>
  <row>
    <panel>
      <table>
        <search id="main_search">
          <query>| makeresults count=10 | eval testcase_id =1 | accum testcase_id</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
          <sampleRatio>1</sampleRatio>
        </search>
        <option name="count">100</option>
        <option name="dataOverlayMode">none</option> 
        
        <option name="drilldown">none</option>
        <option name="percentagesRow">false</option>
        <option name="rowNumbers">false</option>
        <option name="totalsRow">false</option>
        <option name="wrap">true</option>
      </table>
    </panel>
  </row>
  <row>
    <panel>
      <html>
        Testcase Ids: $tkn_testcase_ids$
        <br />
        <br />
        <br />
        <button type="button" id="callRest" value="Call Rest Api">Call Rest Api</button>
      </html>
    </panel>
  </row>
</dashboard>

 

html_button.js

 

require([
    'underscore',
    'splunkjs/mvc',
    'splunkjs/mvc/simplexml/ready!'
], function(_, mvc) {
    var tokens = mvc.Components.get("default");
    $(document).ready(function() {
        var t = setTimeout(function() {
            $("#callRest").click(function() {
                var uri = "API URI";
                $.ajax({
                    type: "POST",
                    url: uri,
                    data: {
                        testcase_ids: tokens.get("tkn_testcase_ids")
                    },
                    beforeSend: function(x) {
                        if (x && x.overrideMimeType) {
                            x.overrideMimeType("application/j-son;charset=UTF-8");
                        }
                    },
                    success: function(rsp) {

                    }
                });
            });
            clearTimeout(t);
        }, 3000);
    })
});

 

Thanks
KV
▄︻̷̿┻̿═━一

If any of my reply helps you to solve the problem Or gain knowledge, an upvote would be appreciated.

Jeeva
Observer

I'm attempting to call a REST API with a button click and display its JSON response on the dashboard. I'm using the following code as a reference. However, I'm not getting any results, and I can see this message in the developer tools console tab:

"Error handling response: TypeError: Cannot read properties of undefined (reading 'mmrConsoleLoggingEnabled')"

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@Jeeva 

Can you please share your sample code?

KV

0 Karma
Get Updates on the Splunk Community!

Continuing Innovation & New Integrations Unlock Full Stack Observability For Your ...

You’ve probably heard the latest about AppDynamics joining the Splunk Observability portfolio, deepening our ...

Monitoring Amazon Elastic Kubernetes Service (EKS)

As we’ve seen, integrating Kubernetes environments with Splunk Observability Cloud is a quick and easy way to ...

Cloud Platform & Enterprise: Classic Dashboard Export Feature Deprecation

As of Splunk Cloud Platform 9.3.2408 and Splunk Enterprise 9.4, classic dashboard export features are now ...