Splunk Search

How retrieve search results via Splunk API?

qcjacobo2577
Path Finder

I recently enabled Splunk tokens (using SAML authentication) and am able to successfully execute basic API calls (such as the one below).

 

curl -H "Authorization: Bearer <token>" -X GET https://<host>.splunkcloud.com:8089/services/authorization/roles

 

I have a Splunk search that works great from the Splunk Cloud UI, but I would like to be able to retrieve the same data in either JSON or CSV format.  I have attempted to follow the Splunk documentation on this, but being brand new may simply be missing something. 

  • What is the best way to achive my goal in this case?
  • Is using Splunk tokens the preferred/best approach?
Labels (1)
Tags (2)
0 Karma
1 Solution

yuanliu
SplunkTrust
SplunkTrust

To perform a search, you first post it to the search endpoint as a job.

curl -H "Authorization: Bearer <token>" https://<host>.splunkcloud.com:8089/services/search/job -d search="my great search"

This will submit "my great search" and return an XML document.  You then use the value of the document's <sid /> segment get the result, like this

curl -H "Authorization: Bearer <token>" --get https://<host>.splunkcloud.com:8089/services/search/job/<sid>/result" -d output_mode=json

Of course, you can use output_mode=json in the first command and use the sid node of the JSON document.

View solution in original post

yuanliu
SplunkTrust
SplunkTrust

To perform a search, you first post it to the search endpoint as a job.

curl -H "Authorization: Bearer <token>" https://<host>.splunkcloud.com:8089/services/search/job -d search="my great search"

This will submit "my great search" and return an XML document.  You then use the value of the document's <sid /> segment get the result, like this

curl -H "Authorization: Bearer <token>" --get https://<host>.splunkcloud.com:8089/services/search/job/<sid>/result" -d output_mode=json

Of course, you can use output_mode=json in the first command and use the sid node of the JSON document.

Get Updates on the Splunk Community!

Introducing the Splunk Community Dashboard Challenge!

Welcome to Splunk Community Dashboard Challenge! This is your chance to showcase your skills in creating ...

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer Certification at ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...