Getting Data In

Not able to obtain search results via Splunk REST API

kiril123
Path Finder

Hello,

I am using Splunk Python SDK to connect to Splunk via REST API.

However the code I have written does not return any results:

query = 'search index = test | stats sum(bytes) as bytes by ip'
kwargs_oneshot = {"earliest_time": "-2m","latest_time": "now","enable_lookups":True,"rf":["ip"]}
job = splunk_connection_service.jobs.create(query, **kwargs_oneshot)
while not job.is_done():
    time.sleep(.2)

for result in results.ResultsReader(job.results()):
    print (result)

If I remove "stats" command from the query, then I only get "_raw" data. What should i change in my code to get the "stats" command output?

Thank you.

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@kiril123
I'm assumed your search is giving proper output on SH.

Have you tried reader?

import splunklib.results as results

...

# Run a one-shot search and display the results using the results reader

# Set the parameters for the search:
# - Search everything in a 24-hour time range starting June 19, 12:00pm
# - Display the first 10 results
kwargs_oneshot = {"earliest_time": "2014-06-19T12:00:00.000-07:00",
                  "latest_time": "2014-06-20T12:00:00.000-07:00"}
searchquery_oneshot = "search * | head 10"

oneshotsearch_results = service.jobs.oneshot(searchquery_oneshot, **kwargs_oneshot)

# Get the results and display them using the ResultsReader
reader = results.ResultsReader(oneshotsearch_results)
for item in reader:
    print(item)

http://dev.splunk.com/view/python-sdk/SP-CAAAER5

0 Karma

kiril123
Path Finder

Yes, i tried that.
When I run 'query = search index = test' i get the output in raw format.
However when I run 'query = search index = test | stats sum(bytes) as bytes by ip' I get none.

Running the search directly on the SH works fine.

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

Can you please try this search in python?

index = test | table _time _raw bytes ip

I just want to know about the fields are there or not. If we didn't find any fields then need to check extraction of fields or app context from which this search is executing.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...