<?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 How to make a python http REST call for kv store? in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/How-to-make-a-python-http-REST-call-for-kv-store/m-p/174921#M2335</link>
    <description>&lt;P&gt;I am trying to make rest call for KV store through python but i am having some issues.&lt;BR /&gt;
I am new to python and rest call hence any help would be great.&lt;BR /&gt;
i am trying to execute the following but i am not able to successfuly make the rest call.&lt;BR /&gt;
am not sure what should be passed with data in the urllib2.Request.&lt;BR /&gt;
I am getting the following exception.&lt;/P&gt;

&lt;H1&gt;exception:&lt;/H1&gt;

&lt;P&gt;Traceback (most recent call last):&lt;BR /&gt;
  File "httptest.py", line 19, in &lt;BR /&gt;
    request = urllib2.Request(base_url + '/servicesNS/nobody/XXX/storage/collections/data/windows_perfmon' % (username),&lt;BR /&gt;
TypeError: not all arguments converted during string formatting&lt;/P&gt;

&lt;H1&gt;python program:&lt;/H1&gt;

&lt;P&gt;import sys&lt;BR /&gt;
import sys, os, random, json, collections, time&lt;BR /&gt;
import urllib, urllib2&lt;BR /&gt;
from xml.dom import minidom&lt;/P&gt;

&lt;P&gt;base_url = '&lt;A href="https://XXX:8089" target="_blank"&gt;https://XXX:8089&lt;/A&gt;'&lt;BR /&gt;
username = 'XX'&lt;BR /&gt;
password = 'XX'&lt;/P&gt;

&lt;P&gt;request = urllib2.Request(base_url + '/servicesNS/%s/search/auth/login' % (username), &lt;BR /&gt;
    data = urllib.urlencode({'username': username, 'password': password}))&lt;BR /&gt;
server_content = urllib2.urlopen(request)&lt;/P&gt;

&lt;P&gt;session_key = minidom.parseString(server_content.read()).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue&lt;/P&gt;

&lt;P&gt;request = urllib2.Request(base_url + '/servicesNS/nobody/myapp/collections/data/windows_perfmon' % (username), &lt;BR /&gt;
    data ,&lt;BR /&gt;
    headers = { 'Authorization': ('Splunk %s' %session_key)})&lt;BR /&gt;
search_results = urllib2.urlopen(request)&lt;BR /&gt;
print search_results.read()&lt;/P&gt;</description>
    <pubDate>Mon, 28 Sep 2020 18:34:51 GMT</pubDate>
    <dc:creator>spyme72</dc:creator>
    <dc:date>2020-09-28T18:34:51Z</dc:date>
    <item>
      <title>How to make a python http REST call for kv store?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-make-a-python-http-REST-call-for-kv-store/m-p/174921#M2335</link>
      <description>&lt;P&gt;I am trying to make rest call for KV store through python but i am having some issues.&lt;BR /&gt;
I am new to python and rest call hence any help would be great.&lt;BR /&gt;
i am trying to execute the following but i am not able to successfuly make the rest call.&lt;BR /&gt;
am not sure what should be passed with data in the urllib2.Request.&lt;BR /&gt;
I am getting the following exception.&lt;/P&gt;

&lt;H1&gt;exception:&lt;/H1&gt;

&lt;P&gt;Traceback (most recent call last):&lt;BR /&gt;
  File "httptest.py", line 19, in &lt;BR /&gt;
    request = urllib2.Request(base_url + '/servicesNS/nobody/XXX/storage/collections/data/windows_perfmon' % (username),&lt;BR /&gt;
TypeError: not all arguments converted during string formatting&lt;/P&gt;

&lt;H1&gt;python program:&lt;/H1&gt;

&lt;P&gt;import sys&lt;BR /&gt;
import sys, os, random, json, collections, time&lt;BR /&gt;
import urllib, urllib2&lt;BR /&gt;
from xml.dom import minidom&lt;/P&gt;

&lt;P&gt;base_url = '&lt;A href="https://XXX:8089" target="_blank"&gt;https://XXX:8089&lt;/A&gt;'&lt;BR /&gt;
username = 'XX'&lt;BR /&gt;
password = 'XX'&lt;/P&gt;

&lt;P&gt;request = urllib2.Request(base_url + '/servicesNS/%s/search/auth/login' % (username), &lt;BR /&gt;
    data = urllib.urlencode({'username': username, 'password': password}))&lt;BR /&gt;
server_content = urllib2.urlopen(request)&lt;/P&gt;

&lt;P&gt;session_key = minidom.parseString(server_content.read()).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue&lt;/P&gt;

&lt;P&gt;request = urllib2.Request(base_url + '/servicesNS/nobody/myapp/collections/data/windows_perfmon' % (username), &lt;BR /&gt;
    data ,&lt;BR /&gt;
    headers = { 'Authorization': ('Splunk %s' %session_key)})&lt;BR /&gt;
search_results = urllib2.urlopen(request)&lt;BR /&gt;
print search_results.read()&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 18:34:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-make-a-python-http-REST-call-for-kv-store/m-p/174921#M2335</guid>
      <dc:creator>spyme72</dc:creator>
      <dc:date>2020-09-28T18:34:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a python http REST call for kv store?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-make-a-python-http-REST-call-for-kv-store/m-p/174922#M2336</link>
      <description>&lt;P&gt;Probably a bit late to help you but this might help others.&lt;/P&gt;

&lt;P&gt;Just trying to work this out myself and came up with the following solutions using a basic library I found here&lt;/P&gt;

&lt;P&gt;&lt;A href="http://docs.python-requests.org/en/latest/index.html"&gt;http://docs.python-requests.org/en/latest/index.html&lt;/A&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;# &lt;A href="http://isbullsh.it/2012/06/Rest-api-in-python/" target="test_blank"&gt;http://isbullsh.it/2012/06/Rest-api-in-python/&lt;/A&gt;
# &lt;A href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html" target="test_blank"&gt;http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html&lt;/A&gt;

import requests, json

# Curl Example
# curl -k -u admin:changeme  &lt;A href="https://localhost:8089/servicesNS/nobody/nvd_datafeeds/storage/collections/data/cve_database" target="test_blank"&gt;https://localhost:8089/servicesNS/nobody/nvd_datafeeds/storage/collections/data/cve_database&lt;/A&gt;  -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
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Seems the others are not very REST friendly&lt;BR /&gt;
&lt;A href="http://isbullsh.it/2012/06/Rest-api-in-python/"&gt;http://isbullsh.it/2012/06/Rest-api-in-python/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2015 02:08:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-make-a-python-http-REST-call-for-kv-store/m-p/174922#M2336</guid>
      <dc:creator>phoenixdigital</dc:creator>
      <dc:date>2015-07-02T02:08:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a python http REST call for kv store?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-make-a-python-http-REST-call-for-kv-store/m-p/174923#M2337</link>
      <description>&lt;P&gt;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.&lt;/P&gt;

&lt;P&gt;You likely have already resolved this issue or have given up. However this answer is for future people looking for an answer.&lt;/P&gt;

&lt;P&gt;This is the solution I just discovered that works quite well using the library&lt;BR /&gt;
&lt;A href="http://docs.python-requests.org/en/latest/index.html"&gt;http://docs.python-requests.org/en/latest/index.html&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Code examples&lt;BR /&gt;
&lt;A href="http://pastebin.com/5LG8YAr1"&gt;http://pastebin.com/5LG8YAr1&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2015 04:45:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-make-a-python-http-REST-call-for-kv-store/m-p/174923#M2337</guid>
      <dc:creator>phoenixdigital</dc:creator>
      <dc:date>2015-07-02T04:45:55Z</dc:date>
    </item>
  </channel>
</rss>

