<?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 Re: Why am I getting &amp;quot;IndexError: list index out of range&amp;quot; trying to get the session key in a Splunk REST API call? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Why-am-I-getting-quot-IndexError-list-index-out-of-range-quot/m-p/258142#M49598</link>
    <description>&lt;P&gt;This is the code that i am using&lt;/P&gt;

&lt;P&gt;import urllib&lt;BR /&gt;
import httplib2&lt;BR /&gt;
import time&lt;BR /&gt;
import re&lt;BR /&gt;
from time import localtime,strftime&lt;BR /&gt;
from xml.dom import minidom&lt;BR /&gt;
import json&lt;BR /&gt;
baseurl = '&lt;A href="https://localhost:8089"&gt;https://localhost:8089&lt;/A&gt;'&lt;BR /&gt;
username = ''&lt;BR /&gt;
password = ''&lt;BR /&gt;
myhttp = httplib2.Http()&lt;/P&gt;

&lt;P&gt;servercontent = myhttp.request(baseurl + '/services/auth/login', 'POST',&lt;BR /&gt;
                            headers={}, body=urllib.urlencode({'username':username, 'password':password}))[1]&lt;BR /&gt;
sessionkey = minidom.parseString(servercontent).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue&lt;BR /&gt;
print "====&amp;gt;sessionkey:  %s  &amp;lt;====" % sessionkey&lt;/P&gt;</description>
    <pubDate>Tue, 06 Oct 2015 06:55:46 GMT</pubDate>
    <dc:creator>nikhiltikoo</dc:creator>
    <dc:date>2015-10-06T06:55:46Z</dc:date>
    <item>
      <title>Why am I getting "IndexError: list index out of range" trying to get the session key in a Splunk REST API call?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Why-am-I-getting-quot-IndexError-list-index-out-of-range-quot/m-p/258139#M49595</link>
      <description>&lt;P&gt;I am trying to get the result of a search from Splunk, but when I try to get the session key, I am getting the following error.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Traceback (most recent call last): File "splunkenter.py", line 18, in sessionkey = minidom.parseString(servercontent).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue IndexError: list index out of range
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Can anybody tell me where is the problem lying? I am very new to API's.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Oct 2015 05:32:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Why-am-I-getting-quot-IndexError-list-index-out-of-range-quot/m-p/258139#M49595</guid>
      <dc:creator>nikhiltikoo</dc:creator>
      <dc:date>2015-10-05T05:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I getting "IndexError: list index out of range" trying to get the session key in a Splunk REST API call?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Why-am-I-getting-quot-IndexError-list-index-out-of-range-quot/m-p/258140#M49596</link>
      <description>&lt;P&gt;Assuming you are writing a scripted input or modular input, you can read the session key  like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;config_str = sys.stdin.read()
doc = xml.dom.minidom.parseString(config_str)
root = doc.documentElement
sessionkey = root.getElementsByTagName("session_key")[0].firstChild.data
config["session_key"] = sessionkey
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The error you are seeing is that there doesn't exist an array for one of the elements. So either &lt;CODE&gt;getElementsByTagName('sessionKey')[0]&lt;/CODE&gt; has no data or &lt;CODE&gt;childNodes[0]&lt;/CODE&gt; has no data. Those would be the two arrays that would cause that error. Try splitting the call apart into different variables and isolate which one is not returning correctly.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Oct 2015 15:53:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Why-am-I-getting-quot-IndexError-list-index-out-of-range-quot/m-p/258140#M49596</guid>
      <dc:creator>alacercogitatus</dc:creator>
      <dc:date>2015-10-05T15:53:07Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I getting "IndexError: list index out of range" trying to get the session key in a Splunk REST API call?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Why-am-I-getting-quot-IndexError-list-index-out-of-range-quot/m-p/258141#M49597</link>
      <description>&lt;P&gt;I am not reading anything from the standard input. I just want to obtain the session key for the on going session.&lt;BR /&gt;
config_str = sys.stdin.read() -&amp;gt; This prompts user to input something.&lt;BR /&gt;
Whereas i don't want to input anything. I am just trying to retrieve the session id for the session in order to connect to the splunk.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2015 06:37:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Why-am-I-getting-quot-IndexError-list-index-out-of-range-quot/m-p/258141#M49597</guid>
      <dc:creator>nikhiltikoo</dc:creator>
      <dc:date>2015-10-06T06:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I getting "IndexError: list index out of range" trying to get the session key in a Splunk REST API call?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Why-am-I-getting-quot-IndexError-list-index-out-of-range-quot/m-p/258142#M49598</link>
      <description>&lt;P&gt;This is the code that i am using&lt;/P&gt;

&lt;P&gt;import urllib&lt;BR /&gt;
import httplib2&lt;BR /&gt;
import time&lt;BR /&gt;
import re&lt;BR /&gt;
from time import localtime,strftime&lt;BR /&gt;
from xml.dom import minidom&lt;BR /&gt;
import json&lt;BR /&gt;
baseurl = '&lt;A href="https://localhost:8089"&gt;https://localhost:8089&lt;/A&gt;'&lt;BR /&gt;
username = ''&lt;BR /&gt;
password = ''&lt;BR /&gt;
myhttp = httplib2.Http()&lt;/P&gt;

&lt;P&gt;servercontent = myhttp.request(baseurl + '/services/auth/login', 'POST',&lt;BR /&gt;
                            headers={}, body=urllib.urlencode({'username':username, 'password':password}))[1]&lt;BR /&gt;
sessionkey = minidom.parseString(servercontent).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue&lt;BR /&gt;
print "====&amp;gt;sessionkey:  %s  &amp;lt;====" % sessionkey&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2015 06:55:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Why-am-I-getting-quot-IndexError-list-index-out-of-range-quot/m-p/258142#M49598</guid>
      <dc:creator>nikhiltikoo</dc:creator>
      <dc:date>2015-10-06T06:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I getting "IndexError: list index out of range" trying to get the session key in a Splunk REST API call?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Why-am-I-getting-quot-IndexError-list-index-out-of-range-quot/m-p/258143#M49599</link>
      <description>&lt;P&gt;Ok, so now you have another array. &lt;CODE&gt;myhttp.request(baseurl + '/services/auth/login', 'POST',&lt;BR /&gt;
                               headers={}, body=urllib.urlencode({'username': username, 'password': password}))&lt;/CODE&gt; now might not have  a &lt;CODE&gt;1&lt;/CODE&gt; index in it. Try using exceptions to figure it out.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;try:
    servercontent = myhttp.request(baseurl + '/services/auth/login', 'POST',
                           headers={}, body=urllib.urlencode({'username': username, 'password': password}))[1]
except:
    print "error in retrieving login."
try:
     sessionkey = minidom.parseString(servercontent).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue
     print "====&amp;gt;sessionkey: %s &amp;lt;====" % sessionkey
except:
     print "error in retrieving sessionkey"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Oct 2015 13:14:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Why-am-I-getting-quot-IndexError-list-index-out-of-range-quot/m-p/258143#M49599</guid>
      <dc:creator>alacercogitatus</dc:creator>
      <dc:date>2015-10-06T13:14:59Z</dc:date>
    </item>
  </channel>
</rss>

