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!

SplunkTrust Application Period is Officially OPEN!

It's that time, folks! The application/nomination period for the 2025 SplunkTrust is officially open! If you ...

Splunk Answers Content Calendar, June Edition II

Get ready to dive into Splunk Dashboard panels this week! We'll be tackling common questions around ...

Splunk Observability Cloud's AI Assistant in Action Series: Auditing Compliance and ...

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