<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to make rest calls from python to modify saved search properties? in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/How-to-make-rest-calls-from-python-to-modify-saved-search/m-p/339270#M5147</link>
    <description>&lt;P&gt;Nice post!&lt;/P&gt;

&lt;P&gt;I am trying to set up my custom endpoint, pretty sure went through restmap.conf and web.xml, python file properly. I can view the endpoint on port 8089 at &lt;A href="https://mysplunk:8089/services/testendpoint"&gt;https://mysplunk:8089/services/testendpoint&lt;/A&gt; and can get the print out from the python file. However, when I tried to call service in dashboard javascript with: &lt;BR /&gt;
     service.post('/services/testendpoint', ...) &lt;BR /&gt;
I got 404 error.  I am using Splunk 7.2.0.&lt;BR /&gt;
I noticed that  you used '/servicesNS/nobody/app_name/saved/searches/receive', Why is it "saved/searches'? I tried the same path and got "Bad Request" error even I don't do anything with the form data. Curious what is your URL on your 8089 port web? I appreciate your response.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Tony&lt;/P&gt;</description>
    <pubDate>Mon, 12 Nov 2018 04:19:40 GMT</pubDate>
    <dc:creator>Dev999</dc:creator>
    <dc:date>2018-11-12T04:19:40Z</dc:date>
    <item>
      <title>How to make rest calls from python to modify saved search properties?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-make-rest-calls-from-python-to-modify-saved-search/m-p/339268#M5145</link>
      <description>&lt;P&gt;Let me start off with what I already have:&lt;BR /&gt;
an XML dashboard&lt;BR /&gt;
a JS script&lt;BR /&gt;
a python handler&lt;BR /&gt;
a restmap.conf&lt;BR /&gt;
a web.conf&lt;/P&gt;

&lt;H2&gt;XML Dashboard&lt;/H2&gt;

&lt;P&gt;Saved-Search Email Change Dashboard&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;panel&amp;gt;
  &amp;lt;html&amp;gt;
    &amp;lt;form id="userPOSTForm"&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;label for="Search"&amp;gt;Search&amp;lt;/label&amp;gt;
        &amp;lt;input type="text" name="Search"/&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;label for="Email"&amp;gt;Email&amp;lt;/label&amp;gt;
        &amp;lt;input type="text" name="Email"/&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button id="postButton"&amp;gt;Submit&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
    &amp;lt;div id="postResponseBox"/&amp;gt;
  &amp;lt;/html&amp;gt;
&amp;lt;/panel&amp;gt;
&amp;lt;panel&amp;gt;
  &amp;lt;title&amp;gt;GET Form&amp;lt;/title&amp;gt;
  &amp;lt;html&amp;gt;
    &amp;lt;form id="userGETForm"&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;label for="Search"&amp;gt;Search&amp;lt;/label&amp;gt;
        &amp;lt;input type="text" name="Search"/&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button id="getButton"&amp;gt;Get Data!&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;
    &amp;lt;div id="getResponseBox"/&amp;gt;
  &amp;lt;/html&amp;gt;
&amp;lt;/panel&amp;gt;  
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H2&gt;  email_change.js&lt;/H2&gt;

&lt;PRE&gt;&lt;CODE&gt;require([
    'underscore',
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/simplexml/ready!'
], function(_, $, mvc) {

    $(document.body).on('click', '#postButton', function(e) {

        e.preventDefault();
        var service = mvc.createService();
        var data = $('#userPOSTForm').serializeArray();
        var data_obj = {};

        _.each(data, function(field) {
            var key = field['name'];
            var value = field['value'];

            if(key == 'Email') {
                data_obj[key] = value.split(',');
            } else {
                data_obj[key] = value;
            }

        });
        alert(data_obj['Search']);
        service.post('/servicesNS/nobody/app_name/saved/searches/send', data_obj, function(err, response) {

            if(err) {
                console.log('error: ', err);
            }
            else if(response.status === 200) {
                console.log('response: ', response);
                $('#postResponseBox').empty();
                $('#postResponseBox').append('&amp;lt;p class=\"successBox\"&amp;gt;Email(s) Updated Successfully&amp;lt;/p&amp;gt;');
                $('#userPOSTForm')[0].reset();
                setTimeout(function() { $('#postResponseBox').empty(); }, 4000);
            }
        });
    });

    $(document.body).on('click', '#getButton', function(e) {

        e.preventDefault();

        var service = mvc.createService();
        var get_data = $('#userGETForm').serializeArray();
        var cleaned_data = {};

            cleanData = function(data) {
            _.each(data, function(field) {
                var key = field['name'];
                var value = field['value'];

                cleaned_data[key] = value;
            });
        }

        cleanData(get_data);

        service.get('/servicesNS/nobody/app_name/saved/searches/receive', cleaned_data, function(err, response) {

            if(err) {
                console.log('error: ', err);
            }
            else if(response.status === 200) {
                console.log('Response: ', response.data);
                var response_data = JSON.parse(response.data);
                $('#getResponseBox').empty();
                $('#getResponseBox').append('Emails configured: ' + response_data.entry[0].content.email);
                $('#userGETForm')[0].reset();
            }
        });
    });
});
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H2&gt;Python handler&lt;/H2&gt;

&lt;P&gt;import splunk&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;class Send(splunk.rest.BaseRestHandler):

    def handle_POST(self):
        sessionKey = self.sessionKey
        search = ''
        email = []

        try:
            payload = self.request['payload']

            for el in payload.split('&amp;amp;'):
                key, value = el.split('=')
                if 'Search' in key:
                    search = value
                if 'Email' in key:
                    email.append(value)

            if search is '':
                self.response.setStatus(400);
                self.response.write('A search must be provided')
            else:
                post_path = '/servicesNS/nobody/app_name/saved/searches/' + search
                new_emails = { 'action.email.to' : email }
                serverContent = splunk.rest.simpleRequest(post_path, sessionKey=sessionKey, postargs=new_emails, method='POST', raiseAllErrors=True)    

        except Exception, e:
            self.response.write(e)

    handle_GET=handle_POST

class Receive(splunk.rest.BaseRestHandler):

    def handle_GET(self):
        sessionKey = self.sessionKey

        try:
            queried_search = self.request['query']['Search']

            get_path = '/servicesNS/nobody/app_name/saved/searches/' + queried_search + "?output_mode=json"

            serverResponse, serverContent = splunk.rest.simpleRequest(get_path, sessionKey=sessionKey, method='GET', raiseAllErrors=True)
            self.response.write(serverContent)

        except Exception, e:
            self.response.write(e)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H2&gt;restmap.conf&lt;/H2&gt;

&lt;PRE&gt;&lt;CODE&gt;[script:email_change_send]
match=/send
handler=email_change.Send

[script:email_change_receive]
match=/receive
handler=email_change.Receive
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H2&gt;web.conf&lt;/H2&gt;

&lt;PRE&gt;&lt;CODE&gt;[expose:email_change_send]
pattern=send
methods=POST

[expose:email_change_receive]
pattern=receive
methods=GET
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I am trying to create a dashboard that can update the email addresses associated with a saved search using POST calls. Also, I want to retrieve the email attached to the saved search via a GET request.&lt;BR /&gt;
What am I doing wrong here?&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2018 12:09:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-make-rest-calls-from-python-to-modify-saved-search/m-p/339268#M5145</guid>
      <dc:creator>snipedown21</dc:creator>
      <dc:date>2018-01-30T12:09:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to make rest calls from python to modify saved search properties?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-make-rest-calls-from-python-to-modify-saved-search/m-p/339269#M5146</link>
      <description>&lt;P&gt;This looks very close to what Hurricane Labs posted&lt;BR /&gt;
(I have a slightly different issue, and I ran across this in my research.)&lt;/P&gt;

&lt;P&gt;&lt;A href="https://www.hurricanelabs.com/splunk-tutorials/splunk-custom-endpoints-part-1-the-basics"&gt;Splunk Custom Endpoints&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Apr 2018 17:36:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-make-rest-calls-from-python-to-modify-saved-search/m-p/339269#M5146</guid>
      <dc:creator>fk319</dc:creator>
      <dc:date>2018-04-23T17:36:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to make rest calls from python to modify saved search properties?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-make-rest-calls-from-python-to-modify-saved-search/m-p/339270#M5147</link>
      <description>&lt;P&gt;Nice post!&lt;/P&gt;

&lt;P&gt;I am trying to set up my custom endpoint, pretty sure went through restmap.conf and web.xml, python file properly. I can view the endpoint on port 8089 at &lt;A href="https://mysplunk:8089/services/testendpoint"&gt;https://mysplunk:8089/services/testendpoint&lt;/A&gt; and can get the print out from the python file. However, when I tried to call service in dashboard javascript with: &lt;BR /&gt;
     service.post('/services/testendpoint', ...) &lt;BR /&gt;
I got 404 error.  I am using Splunk 7.2.0.&lt;BR /&gt;
I noticed that  you used '/servicesNS/nobody/app_name/saved/searches/receive', Why is it "saved/searches'? I tried the same path and got "Bad Request" error even I don't do anything with the form data. Curious what is your URL on your 8089 port web? I appreciate your response.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Tony&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 04:19:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-make-rest-calls-from-python-to-modify-saved-search/m-p/339270#M5147</guid>
      <dc:creator>Dev999</dc:creator>
      <dc:date>2018-11-12T04:19:40Z</dc:date>
    </item>
  </channel>
</rss>

