<?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 POST Saved Search XML/JSON Definition to REST API in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/How-to-POST-Saved-Search-XML-JSON-Definition-to-REST-API/m-p/334732#M61898</link>
    <description>&lt;P&gt;@Rikh Did you got this problem solved? If yes can you please post a solution here .&lt;/P&gt;</description>
    <pubDate>Mon, 17 Dec 2018 23:58:36 GMT</pubDate>
    <dc:creator>nilendra19888</dc:creator>
    <dc:date>2018-12-17T23:58:36Z</dc:date>
    <item>
      <title>How to POST Saved Search XML/JSON Definition to REST API</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-POST-Saved-Search-XML-JSON-Definition-to-REST-API/m-p/334731#M61897</link>
      <description>&lt;P&gt;I can GET the definition of a saved search (report) from our dev server with a call like&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;curl -k -u me:word &lt;A href="https://splunk-for-dev:8089/serviceNS/me/my-app/saved/searches/my-report" target="test_blank"&gt;https://splunk-for-dev:8089/serviceNS/me/my-app/saved/searches/my-report&lt;/A&gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;How do I use the resulting XML/JSON to POST to our prod server?  The closest that I've found is something like&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;curl -k -u me:word &lt;A href="https://splunk-for-prod:8089/serviceNS/me/my-app/saved/searches" target="test_blank"&gt;https://splunk-for-prod:8089/serviceNS/me/my-app/saved/searches&lt;/A&gt; \
    -d name=my-report -d search=...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But that means going through the XML/JSON and working out which are the non-default values and a whole lot of text munging.  Surely there is a way that I can just post the XML/JSON that I've already got?&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2018 13:27:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-POST-Saved-Search-XML-JSON-Definition-to-REST-API/m-p/334731#M61897</guid>
      <dc:creator>RikH</dc:creator>
      <dc:date>2018-01-25T13:27:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to POST Saved Search XML/JSON Definition to REST API</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-POST-Saved-Search-XML-JSON-Definition-to-REST-API/m-p/334732#M61898</link>
      <description>&lt;P&gt;@Rikh Did you got this problem solved? If yes can you please post a solution here .&lt;/P&gt;</description>
      <pubDate>Mon, 17 Dec 2018 23:58:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-POST-Saved-Search-XML-JSON-Definition-to-REST-API/m-p/334732#M61898</guid>
      <dc:creator>nilendra19888</dc:creator>
      <dc:date>2018-12-17T23:58:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to POST Saved Search XML/JSON Definition to REST API</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-POST-Saved-Search-XML-JSON-Definition-to-REST-API/m-p/334733#M61899</link>
      <description>&lt;P&gt;Do you know how to write an API call to get the result in UI?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jan 2019 22:09:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-POST-Saved-Search-XML-JSON-Definition-to-REST-API/m-p/334733#M61899</guid>
      <dc:creator>tejureddy101</dc:creator>
      <dc:date>2019-01-03T22:09:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to POST Saved Search XML/JSON Definition to REST API</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-POST-Saved-Search-XML-JSON-Definition-to-REST-API/m-p/334734#M61900</link>
      <description>&lt;P&gt;Sometimes back I did like below, I basically had to write a python code to achieve this, I am posting a sample code,&lt;BR /&gt;
Please note that its a very primitive code where exception handling is not proper and code modularise  is not there. Let me briefly explain what it does,&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;I have a alert in my local called "demo"&lt;/LI&gt;
&lt;LI&gt;First I am getting the alert json payload using python request module (I am able to convert the cURL request to python here). &lt;/LI&gt;
&lt;LI&gt;Now here the drama started while I am posting it to splunk. First of all I am not able to convert the cURL post request to python, even sending the data through dictionary also not working. So I directly called cURL in python. Now the second challange was the payload. Only the content payload itself so heavy that if I pass it to curl its not able to handle. Thats why I thought I will only send those data which user is able to change from UI. Thats why key_list comes into picture. The list is not exhaustive. &lt;/LI&gt;
&lt;LI&gt;Now the code is pusing the "demo" alert payload to a new alert called "sid". Thats why I have changed the name(hardcoded). So in the post request we need to give the new server url. &lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;I haven't handle the scenario where if the alert exists it should update otherwise it should create a new alert. I think that should be easy to implement. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import requests as req
import json
import shlex
import subprocess

def get_alert_dtl_frm_splunk(requestURL,parameters,auth):
    response=req.get(url=requestURL,params=parameters,verify=False,auth=auth)
    if response.status_code !=200:
        print('Status: ',response.status_code,'Headers: ',response.headers,'Error Response: ',response.json())
        exit()
    data=response.json()
    return json.dumps(data)

def main():
    #preparing the data for get request
    requestURL = 'https://localhost:8089/servicesNS/admin/tmdb/saved/searches/demo'
    params = (('output_mode', 'json'),)
    auth=('admin', 'monitor!')

    #get the alert json from one splunk instance
    data = get_alert_dtl_frm_splunk(requestURL,params,auth)
    data = json.loads(data)
    alert_content_json = data["entry"][0]["content"]
    #print(data)

    #post to splunk
    alert_content_json["name"] = "Sid"
    cmd ='curl -k -u admin:monitor! &lt;A href="https://localhost:8089/servicesNS/admin/tmdb/saved/searches'" target="test_blank"&gt;https://localhost:8089/servicesNS/admin/tmdb/saved/searches'&lt;/A&gt;;
    cmd = cmd + " --data-urlencode name=" + alert_content_json["name"] #as first argument of curl has to be name
    key_list = ["alert.severity","alert.suppress","alert.track","alert_type","cron_schedule","is_scheduled","alert_threshold","alert_comparator","search"] #need to do this as cUrl command is not able to send the full payload
    for key in alert_content_json.keys():
        if key in key_list:
            value = str(alert_content_json[key])
            value = value.replace("\"","\"\"")
            value = value.replace("\\n","\\")
            cmd = cmd + " --data-urlencode "  + key +  '="'  + value + "\"" 
    #print(cmd)
    args = shlex.split(cmd)
    process = subprocess.call(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()

main()
&lt;/CODE&gt;&lt;/PRE&gt;&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Fri, 04 Jan 2019 05:09:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-POST-Saved-Search-XML-JSON-Definition-to-REST-API/m-p/334734#M61900</guid>
      <dc:creator>sdchakraborty</dc:creator>
      <dc:date>2019-01-04T05:09:21Z</dc:date>
    </item>
  </channel>
</rss>

