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
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...