@rohithvr19 Your script won't work on my machine so I have created a sample script which returns simple "Hello world" text on click of dashboard button. You just create a similar configuration and ...
See more...
@rohithvr19 Your script won't work on my machine so I have created a sample script which returns simple "Hello world" text on click of dashboard button. You just create a similar configuration and Python file as per your requirement. Below is the code and file/folder structure. hello_world.py import splunk, sys
from json import dumps
class HelloWorld(splunk.rest.BaseRestHandler):
'''
Class for service custom endpoint.
'''
def handle_POST(self):
'''
This endpoint handler
:return: None
'''
payload = {
"text": "Hello world!"
}
response = dumps({"data": payload, "status": "OK", "error": "None"})
self.response.setHeader('content-type', 'application/json')
self.response.write(response)
#handle verbs, otherwise Splunk will throw an error
handle_GET = handle_POST
restmap.conf [script:my_custom_endpoint]
match = /my_custom_endpoint
handler = hello_world.HelloWorld web.conf [expose:my_custom_endpoint]
pattern = my_custom_endpoint
methods = GET, POST XML <dashboard script="fetch_data.js" version="1.1">
<label>My Dashboard</label>
<description>Dynamic Result Example</description>
<row>
<panel>
<html>
<div>
<button id="fetch-data-button">Fetch Data</button>
<div id="div_result" style="margin-top: 10px; border: 1px solid #ccc; padding: 10px;">Result will be displayed here.</div>
</div>
</html>
</panel>
</row>
</dashboard> fetch_data.js require([
'jquery',
'splunkjs/mvc',
'splunkjs/mvc/simplexml/ready!'
], function($, mvc) {
$('#fetch-data-button').on('click', function() {
var service = mvc.createService();
service.post('/services/my_custom_endpoint', {}, function (err, response) {
console.log(response);
console.log(response.data);
console.log(response.data.data);
console.log(response.data.data.text);
$('#div_result').html(response.data.data.text);
});
return false;
});
}); Screenshot Try this code to learn and understand the custom endpoint and develop a new endpoint as per your needs. I hope this will help you. Thanks KV An upvote would be appreciated if any of my replies help you solve the problem or gain knowledge.