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/

Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...