<?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: a few tips on calling REST endpoints in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/a-few-tips-on-calling-REST-endpoints/m-p/82017#M16948</link>
    <description>&lt;P&gt;'answering' this so it stops showing up in the 'double points' list. &lt;/P&gt;</description>
    <pubDate>Sun, 21 Dec 2014 19:12:12 GMT</pubDate>
    <dc:creator>piebob</dc:creator>
    <dc:date>2014-12-21T19:12:12Z</dc:date>
    <item>
      <title>a few tips on calling REST endpoints</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/a-few-tips-on-calling-REST-endpoints/m-p/82016#M16947</link>
      <description>&lt;P&gt;Not technically a question, but pretty sure will be helpful to many.  If not helpful to &lt;EM&gt;you&lt;/EM&gt;, please don't upvote.&lt;/P&gt;

&lt;HR /&gt;

&lt;H3&gt;What Is REST?&lt;/H3&gt;

&lt;P&gt;REST is basically:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;a scheme to name and organize endpoints, where an &lt;EM&gt;endpoint&lt;/EM&gt; is a server URI; &lt;/LI&gt;
&lt;LI&gt;a convention for client to pass GET/POST/DELETE HTTP requests to an endpoint; &lt;/LI&gt;
&lt;LI&gt;a way to bind server &lt;EM&gt;handlers&lt;/EM&gt; to endpoints, to respond to client requests with a bunch of XML &lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;In other words, YA technology roughly along the lines of CORBA, XML-RPC, SOAP, etc.&lt;/P&gt;

&lt;H3&gt;Basic Usage&lt;/H3&gt;

&lt;UL&gt;
&lt;LI&gt;to create object &lt;EM&gt;foo&lt;/EM&gt; of type &lt;EM&gt;widget&lt;/EM&gt;: &lt;TT&gt;services/.../widgets -d name=foo&lt;/TT&gt;&lt;/LI&gt;
&lt;LI&gt;to list object &lt;EM&gt;foo&lt;/EM&gt; of type &lt;EM&gt;widget&lt;/EM&gt;: &lt;TT&gt;services/.../widgets/foo&lt;/TT&gt;&lt;/LI&gt;
&lt;LI&gt;to edit object &lt;EM&gt;foo&lt;/EM&gt; of type &lt;EM&gt;widget&lt;/EM&gt;: &lt;TT&gt;services/.../widgets/foo -d someAttr=someVal -X POST&lt;/TT&gt;&lt;/LI&gt;
&lt;LI&gt;to delete object &lt;EM&gt;foo&lt;/EM&gt; of type &lt;EM&gt;widget&lt;/EM&gt;: &lt;TT&gt;services/.../widgets/foo -X DELETE&lt;/TT&gt;&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;&lt;CODE&gt;-X POST&lt;/CODE&gt; and &lt;CODE&gt;-X DELETE&lt;/CODE&gt; are options to &lt;CODE&gt;curl(1)&lt;/CODE&gt;; respective alternate forms are &lt;CODE&gt;--post&lt;/CODE&gt; and &lt;CODE&gt;--delete&lt;/CODE&gt;.&lt;/P&gt;

&lt;H3&gt;How to Force App Context&lt;/H3&gt;

&lt;P&gt;Prepend path with &lt;CODE&gt;/servicesNS/nobody/&lt;/CODE&gt;&lt;EM&gt;your-appname&lt;/EM&gt;&lt;CODE&gt;/&lt;/CODE&gt; instead of just &lt;CODE&gt;/services&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;(Useful if you want the REST call to "come from" a particular app.)&lt;/P&gt;

&lt;H3&gt;Logging&lt;/H3&gt;

&lt;P&gt;As of 6.0, tracing to &lt;CODE&gt;splunkd.log&lt;/CODE&gt; may be requested by setting the special logger &lt;CODE&gt;REST_Calls&lt;/CODE&gt; to &lt;CODE&gt;DEBUG&lt;/CODE&gt;. &lt;/P&gt;

&lt;P&gt;All attempted calls will be traced, regardless of their eventual success.&lt;/P&gt;

&lt;P&gt;For each call, we'll trace:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;app name (&lt;TT&gt;search&lt;/TT&gt;, &lt;TT&gt;system&lt;/TT&gt;, &lt;TT&gt;unix&lt;/TT&gt;, ...)&lt;/LI&gt;
&lt;LI&gt;HTTP method (&lt;TT&gt;GET&lt;/TT&gt;, &lt;TT&gt;POST&lt;/TT&gt;, ...)&lt;/LI&gt;
&lt;LI&gt;REST path (&lt;TT&gt;data/indexes&lt;/TT&gt;, &lt;TT&gt;deployment/server/clients&lt;/TT&gt;, ...)&lt;/LI&gt;
&lt;LI&gt;custom action (&lt;TT&gt;countRecentDownloads&lt;/TT&gt;, ...): if applicable&lt;/LI&gt;
&lt;LI&gt;the special &lt;TT&gt;id&lt;/TT&gt; caller argument (for &lt;TT&gt;data/indexes/eendex&lt;/TT&gt;, that'd be &lt;TT&gt;eendex&lt;/TT&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt; if any&lt;/LI&gt;
&lt;LI&gt;remaining caller arguments: if any&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;Example (see below for explanation of &lt;TT&gt;kerl&lt;/TT&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;
&lt;TT&gt;$ kerl data/indexes/foo -d maxMemMB=6&lt;/TT&gt;&lt;BR /&gt;
&lt;BR /&gt;gives&lt;BR /&gt;&lt;BR /&gt;
&lt;TT&gt;10-03-2013 15:12:31.585 -0700 DEBUG REST_Calls - app=search POST data/indexes/foo id=foo: maxMemMB -&amp;gt; [6]&lt;/TT&gt;&lt;/P&gt;

&lt;P&gt;For result code (200, 409, ...) and response elapsed time, you'll need to look at &lt;TT&gt;splunkd_access.log&lt;/TT&gt;&lt;/P&gt;

&lt;H3&gt;Calling via &lt;TT&gt;curl(1)&lt;/TT&gt;, then Sifting through Results to "Separate Wheat from Chaff", Is a Hassle.  Help?&lt;/H3&gt;

&lt;P&gt;The following &lt;TT&gt;bash&lt;/TT&gt; script, known to work also under Windows via MSYS or Cygwin, may be just the thing.  To get usage anytime, just invoke without any arguments, like so (say we named the script &lt;TT&gt;kerl&lt;/TT&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;
&lt;PRE&gt;&lt;BR /&gt;
$ kerl&lt;BR /&gt;
USAGE: /home/v/kerl [-v] [--bare] [--app &amp;lt;appName&amp;gt;] [-u &amp;lt;username&amp;gt;] [-p &amp;lt;password&amp;gt;]&lt;BR /&gt;
                    [&amp;lt;IP|hostname&amp;gt; &amp;lt;port&amp;gt;] &amp;lt;REST path&amp;gt; [REST_arg_0 ... REST_arg_N]&lt;BR /&gt;
                    [curl options like --get, -i, ...]&lt;BR /&gt;
&lt;/PRE&gt;&lt;BR /&gt;
Some sample usages:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;On same host as the Splunk server, default username + password, just get index names.  (Note that the script is smart enough to pick up your &lt;TT&gt;web.conf/[settings]/mgmtPort&lt;/TT&gt; setting.)
&lt;PRE&gt;
$ kerl --bare data/indexes
&amp;lt;title&amp;gt;_audit&amp;lt;/title&amp;gt;
&amp;lt;title&amp;gt;_blocksignature&amp;lt;/title&amp;gt;
&amp;lt;title&amp;gt;_internal&amp;lt;/title&amp;gt;
&amp;lt;title&amp;gt;_thefishbucket&amp;lt;/title&amp;gt;
&amp;lt;title&amp;gt;history&amp;lt;/title&amp;gt;
&amp;lt;title&amp;gt;main&amp;lt;/title&amp;gt;
&amp;lt;title&amp;gt;os&amp;lt;/title&amp;gt;
&amp;lt;title&amp;gt;sample&amp;lt;/title&amp;gt;
&amp;lt;title&amp;gt;splunklogger&amp;lt;/title&amp;gt;
&amp;lt;title&amp;gt;summary&amp;lt;/title&amp;gt;
&lt;/PRE&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Same, but also show the actual &lt;TT&gt;curl(1)&lt;/TT&gt; command being generated &amp;amp; invoked&lt;BR /&gt;
&lt;PRE&gt;&lt;BR /&gt;
$ kerl -v --bare data/indexes&lt;/PRE&gt;&lt;/P&gt;

&lt;P&gt;Running:    curl  -s -S -k -u admin:changeme &lt;A href="https://127.0.0.1:13542/services/data/indexes" target="_blank"&gt;https://127.0.0.1:13542/services/data/indexes&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;&amp;lt;title&amp;gt;_audit&amp;lt;/title&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;_blocksignature&amp;lt;/title&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;_internal&amp;lt;/title&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;_thefishbucket&amp;lt;/title&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;history&amp;lt;/title&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;main&amp;lt;/title&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;os&amp;lt;/title&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;sample&amp;lt;/title&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;splunklogger&amp;lt;/title&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;summary&amp;lt;/title&amp;gt;&lt;BR /&gt;
&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;As app &lt;TT&gt;search&lt;/TT&gt;, add a serverclass. (Note how all the boilerplate about ACLs, etc etc, is filtered out for you.)&lt;BR /&gt;
&lt;PRE&gt;&lt;BR /&gt;
$ kerl --app search deployment/server/serverclasses -d name=foo&lt;BR /&gt;
&amp;lt;feed ...&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;serverclasses&amp;lt;/title&amp;gt;&lt;BR /&gt;
&amp;lt;entry&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;foo&amp;lt;/title&amp;gt;&lt;BR /&gt;
    &amp;lt;blacklist-size&amp;gt;0&lt;BR /&gt;
    &amp;lt;currentDownloads&amp;gt;0&lt;BR /&gt;
    &amp;lt;machineTypesFilter&amp;gt;&lt;BR /&gt;
    &amp;lt;repositoryList&amp;gt;&lt;BR /&gt;
        - app_five&lt;BR /&gt;
        - app_four&lt;BR /&gt;
        - app_three&lt;BR /&gt;
    &amp;lt;repositoryLocation&amp;gt;/home/v/inst/bubbles/etc/deployment-apps&lt;BR /&gt;
    &amp;lt;restartSplunkWeb&amp;gt;0&lt;BR /&gt;
    &amp;lt;restartSplunkd&amp;gt;0&lt;BR /&gt;
    &amp;lt;stateOnClient&amp;gt;enabled&lt;BR /&gt;
    &amp;lt;whitelist-size&amp;gt;0&lt;BR /&gt;
&amp;lt;/entry&amp;gt;&lt;BR /&gt;
&amp;lt;/feed&amp;gt;&lt;BR /&gt;
&lt;/PRE&gt;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Query remote DC (deployment client), non-default username + password, ask for HTTP header&lt;BR /&gt;
&lt;PRE&gt;&lt;BR /&gt;
$ kerl -u admax -p changethee 10.1.42.3 13542 deployment/client --get -i&lt;BR /&gt;
HTTP/1.1 200 OK&lt;BR /&gt;
Connection: Keep-Alive&lt;BR /&gt;
Date: Thu, 03 Oct 2013 22:03:02 GMT&lt;BR /&gt;
Expires: Thu, 26 Oct 1978 00:00:00 GMT&lt;BR /&gt;
Cache-Control: no-store, no-cache, must-revalidate, max-age=0&lt;BR /&gt;
Content-Type: text/xml; charset=UTF-8&lt;BR /&gt;
X-Content-Type-Options: nosniff&lt;BR /&gt;
Content-Length: 3650&lt;BR /&gt;
Vary: Authorization&lt;BR /&gt;
X-Frame-Options: SAMEORIGIN&lt;BR /&gt;
Server: Splunkd&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;
&amp;lt;feed ...&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;deploymentclient&amp;lt;/title&amp;gt;&lt;BR /&gt;
&amp;lt;entry&amp;gt;&lt;BR /&gt;
&amp;lt;title&amp;gt;config&amp;lt;/title&amp;gt;&lt;BR /&gt;
    &amp;lt;disabled&amp;gt;0&lt;BR /&gt;
    &amp;lt;serverClasses&amp;gt;&lt;BR /&gt;
      &amp;lt;s:list&amp;gt;&lt;BR /&gt;
        &amp;lt;s:item&amp;gt;sc_a:app_one&amp;lt;/s:item&amp;gt;&lt;BR /&gt;
        &amp;lt;s:item&amp;gt;sc_a:app_three&amp;lt;/s:item&amp;gt;&lt;BR /&gt;
        &amp;lt;s:item&amp;gt;sc_c:app_five&amp;lt;/s:item&amp;gt;&lt;BR /&gt;
        &amp;lt;s:item&amp;gt;sc_c:app_one&amp;lt;/s:item&amp;gt;&lt;BR /&gt;
      &amp;lt;/s:list&amp;gt;&lt;BR /&gt;
    &amp;lt;targetUri&amp;gt;wimpy.splunk.com:13542&lt;BR /&gt;
&amp;lt;/entry&amp;gt;&lt;BR /&gt;
&amp;lt;/feed&amp;gt;&lt;BR /&gt;
&lt;/PRE&gt;&lt;/P&gt;&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;And here's the script code:&lt;BR /&gt;
&lt;PRE&gt;&lt;BR /&gt;
#!/bin/bash&lt;BR /&gt;
# V 30mar2013&lt;BR /&gt;
# (c) 2013 by Splunk, Inc.&lt;/PRE&gt;&lt;/P&gt;

&lt;P&gt;[ $# -lt 1 ] &amp;amp;&amp;amp; echo 'USAGE: [-v] [--bare] [-u &amp;lt;username&amp;gt;] [-p &amp;lt;password&amp;gt;] [&amp;lt;IP|hostname&amp;gt; &amp;lt;port&amp;gt;] &amp;lt;REST path&amp;gt; [REST_arg_0 ... REST_arg_N] [curl options like --get, -i, ...]' &amp;gt;&amp;amp;2 &amp;amp;&amp;amp; exit 1&lt;BR /&gt;
[ -z "$SPLUNK_HOME" ] &amp;amp;&amp;amp; echo 'SPLUNK_HOME must be set' &amp;gt;&amp;amp;2 &amp;amp;&amp;amp; exit 2&lt;/P&gt;

&lt;P&gt;# # # Examples of acceptable REST Paths + Args&lt;BR /&gt;
# services&lt;BR /&gt;
# data/indexes/*/roll-hot-buckets&lt;BR /&gt;
# data/indexes/ -d name=vooble&lt;BR /&gt;
# data/indexes/vooble&lt;BR /&gt;
# deployment/server/applications?search=NOT%20serverclasses&lt;/P&gt;

&lt;P&gt;# # # Does user want to know exact curl(1) command string?&lt;BR /&gt;
verbose=false&lt;BR /&gt;
if [ $1 == '-v' ]; then&lt;BR /&gt;
    verbose=true&lt;BR /&gt;
    shift&lt;BR /&gt;
fi&lt;/P&gt;

&lt;P&gt;# # # Does user want only bare-bones summary of results?&lt;BR /&gt;
summarizeSedCommand='p'&lt;BR /&gt;
if [ $1 == '--bare' ]; then&lt;BR /&gt;
    # what this sed script does: only print a line, if the immediately preceding line contained the 'entry' begin-tag.&lt;BR /&gt;
    summarizeSedCommand='x; /&amp;lt;entry&amp;gt;/ {x;p;x}'&lt;BR /&gt;
    shift&lt;BR /&gt;
fi&lt;/P&gt;

&lt;P&gt;# # # Decide auth.&lt;BR /&gt;
username='admin'&lt;BR /&gt;
if [ $1 == '-u' ]; then&lt;BR /&gt;
    username="$2"&lt;BR /&gt;
    shift 2&lt;BR /&gt;
fi&lt;/P&gt;

&lt;P&gt;password='changeme'&lt;BR /&gt;
if [ $1 == '-p' ]; then&lt;BR /&gt;
    password="$2"&lt;BR /&gt;
    shift 2&lt;BR /&gt;
fi&lt;/P&gt;

&lt;P&gt;auth="$username:$password"&lt;/P&gt;

&lt;P&gt;# # # Decide host + port.&lt;BR /&gt;
if [[ $# -ge 3 &amp;amp;&amp;amp; $1 =~ ^[[:alnum:]-]?[[:alnum:]\.-]*[[:alnum:]-]?$ &amp;amp;&amp;amp; $2 =~ ^[1-9][0-9]*$ &amp;amp;&amp;amp; $2 -ge 1 &amp;amp;&amp;amp; $2 -le 65535 ]]; then&lt;BR /&gt;
    # Query remote splunkd?&lt;BR /&gt;
    mgmtHostPort="$1:$2"&lt;BR /&gt;
    shift 2&lt;BR /&gt;
else&lt;BR /&gt;
    # Query local splunkd?&lt;BR /&gt;
    mgmtHostPort=`awk '/^mgmtHostPort/ {print $3}' $SPLUNK_HOME/etc/system/local/web.conf 2&amp;gt;/dev/null`&lt;BR /&gt;
    # server.conf corrupt?&lt;BR /&gt;
    [ -z $mgmtHostPort ] &amp;amp;&amp;amp; mgmtHostPort='127.0.0.1:8089'&lt;BR /&gt;
fi&lt;/P&gt;

&lt;P&gt;# # # Decide REST path.&lt;BR /&gt;
path=${1#/} # delete leading '/' if found&lt;BR /&gt;
shift # $@ will now contain just REST args&lt;BR /&gt;
[ ${path:0:8} != 'services' -a ${path:0:10} != 'servicesNS' ] &amp;amp;&amp;amp; path="services/$path"&lt;/P&gt;

&lt;P&gt;# # # Combine host+port and REST path, to get URL.&lt;BR /&gt;
url="https://$mgmtHostPort/$path"&lt;/P&gt;

&lt;P&gt;# # # Compose output-editing script.&lt;BR /&gt;
removeSingleLineA='/This is to override browser formatting|&amp;lt;link href|&amp;lt;updated&amp;gt;|"loadTime"|&amp;lt;opensearch:|&amp;lt;generator |xml-stylesheet |xml version=/ d'&lt;BR /&gt;
removeSingleLineB='/&amp;lt;s:messages\/&amp;gt;|&amp;lt;content |&amp;lt;\/content&amp;gt;|&amp;lt;s:dict&amp;gt;|&amp;lt;\/s:dict&amp;gt;|&amp;lt;id&amp;gt;http/ d'&lt;BR /&gt;
removeRange='/&amp;lt;author&amp;gt;/,/&amp;lt;\/author&amp;gt;/ d;/&amp;lt;s:key name="eai:acl"&amp;gt;/,/^        &amp;lt;\/s:key&amp;gt;/ d;/&amp;lt;s:key name="eai:attributes"&amp;gt;/,/^        &amp;lt;\/s:key&amp;gt;/ d'&lt;BR /&gt;
editWithinLine='/&amp;lt;feed / s/ xmlns=.*$/ ...&amp;gt;/;s/s:key name=//;s/&amp;lt;\/s:key&amp;gt;//;s/"//g;/\/&amp;gt;$/ {s/&amp;lt;/- /;s/\/&amp;gt;//}'&lt;BR /&gt;
removeBlankLine='/^[ ]+$/ d'&lt;/P&gt;

&lt;P&gt;# # # Run.&lt;BR /&gt;
$verbose &amp;amp;&amp;amp; echo -e "\n\tRunning:\tcurl $@ -s -S -k -u $auth ${url//%20/ }\n" &amp;gt;&amp;amp;2&lt;BR /&gt;
curl $@ -s -S -k -u $auth $url | sed -r "$removeSingleLineA;$removeSingleLineB;$removeRange;$editWithinLine;$removeBlankLine" | sed -n "$summarizeSedCommand"&lt;BR /&gt;
&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 14:53:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/a-few-tips-on-calling-REST-endpoints/m-p/82016#M16947</guid>
      <dc:creator>V_at_Splunk</dc:creator>
      <dc:date>2020-09-28T14:53:17Z</dc:date>
    </item>
    <item>
      <title>Re: a few tips on calling REST endpoints</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/a-few-tips-on-calling-REST-endpoints/m-p/82017#M16948</link>
      <description>&lt;P&gt;'answering' this so it stops showing up in the 'double points' list. &lt;/P&gt;</description>
      <pubDate>Sun, 21 Dec 2014 19:12:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/a-few-tips-on-calling-REST-endpoints/m-p/82017#M16948</guid>
      <dc:creator>piebob</dc:creator>
      <dc:date>2014-12-21T19:12:12Z</dc:date>
    </item>
  </channel>
</rss>

