Splunk Dev

How to make a python http REST call for kv store?

spyme72
Path Finder

I am trying to make rest call for KV store through python but i am having some issues.
I am new to python and rest call hence any help would be great.
i am trying to execute the following but i am not able to successfuly make the rest call.
am not sure what should be passed with data in the urllib2.Request.
I am getting the following exception.

exception:

Traceback (most recent call last):
File "httptest.py", line 19, in
request = urllib2.Request(base_url + '/servicesNS/nobody/XXX/storage/collections/data/windows_perfmon' % (username),
TypeError: not all arguments converted during string formatting

python program:

import sys
import sys, os, random, json, collections, time
import urllib, urllib2
from xml.dom import minidom

base_url = 'https://XXX:8089'
username = 'XX'
password = 'XX'

request = urllib2.Request(base_url + '/servicesNS/%s/search/auth/login' % (username),
data = urllib.urlencode({'username': username, 'password': password}))
server_content = urllib2.urlopen(request)

session_key = minidom.parseString(server_content.read()).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue

request = urllib2.Request(base_url + '/servicesNS/nobody/myapp/collections/data/windows_perfmon' % (username),
data ,
headers = { 'Authorization': ('Splunk %s' %session_key)})
search_results = urllib2.urlopen(request)
print search_results.read()

Tags (3)
0 Karma

phoenixdigital
Builder

OK it seems my previous post got lost in mod approval limbo. Likely due to the wall of code I had in the post. So here goes the second attempt with the code up on pastebin.

You likely have already resolved this issue or have given up. However this answer is for future people looking for an answer.

This is the solution I just discovered that works quite well using the library
http://docs.python-requests.org/en/latest/index.html

Code examples
http://pastebin.com/5LG8YAr1

0 Karma

phoenixdigital
Builder

Probably a bit late to help you but this might help others.

Just trying to work this out myself and came up with the following solutions using a basic library I found here

http://docs.python-requests.org/en/latest/index.html

# http://isbullsh.it/2012/06/Rest-api-in-python/
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

import requests, json

# Curl Example
# curl -k -u admin:changeme  https://localhost:8089/servicesNS/nobody/nvd_datafeeds/storage/collections/data/cve_database  -H "Content-Type: application/json" -d '{"cve":"test","cwe":"test","cvss:score":"here","date-published":"test","date-modified":"test","cvss:access-vector":"test","summary":"test"}'

# QUERY COLLECTION
print "\n************** QUERY EXAMPLE ****************"

github_url = "https://localhost:8089/servicesNS/nobody/nvd_datafeeds/storage/collections/data/cve_database"
r = requests.get(github_url, auth=("admin", "changeme"), verify=False)

print 'Status Code %d' % r.status_code
# print r.json
print r.text


# INSERT Data 
print "\n************** INSERT EXAMPLE ****************"

github_url = "https://localhost:8089/servicesNS/nobody/nvd_datafeeds/storage/collections/data/cve_database"
headers = {'Content-Type': 'application/json'}
data = json.dumps({"cve":"test","cwe":"test","cvss:score":"here","date-published":"test","date-modified":"test","cvss:access-vector":"test","summary":"test"}) 
r = requests.post(github_url, data, auth=("admin", "changeme"), verify=False, headers=headers)

print 'Status Code %d' % r.status_code
if r.status_code == 201:
    # print r.json
    print r.text

    # load up data into json
    myData = json.loads(r.text)
    print 'The key is - %s' % myData['_key']

    # DELETE Data we just added above
    print "\n************** DELETE EXAMPLE ****************"

    github_url = "https://localhost:8089/servicesNS/nobody/nvd_datafeeds/storage/collections/data/cve_database/%s" % myData['_key']
    headers = {'Content-Type': 'application/json'}
    r = requests.delete(github_url, auth=("admin", "changeme"), verify=False, headers=headers)

    print 'Status Code %d' % r.status_code
    # print r.json
    print r.text

Seems the others are not very REST friendly
http://isbullsh.it/2012/06/Rest-api-in-python/

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 ...