<?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: How do I unzip a file when pulling it from REST API? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328659#M60989</link>
    <description>&lt;P&gt;This is my version of the code:&lt;/P&gt;

&lt;P&gt;class ZipFileResponseHandler:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def __init__(self,**args):
    pass

def __call__(self, response_object, raw_response_output, response_type, req_args, endpoint):
    file = zipfile.ZipFile(StringIO.StringIO(response_object.content))
    for name in file.namelist():
        if "ENDPOINT" in name:
            data =file.read(name)
            data = data.split('\n')
            for element in data[1:]:
                print_xml_stream(element)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 14 Nov 2017 21:28:57 GMT</pubDate>
    <dc:creator>tamduong16</dc:creator>
    <dc:date>2017-11-14T21:28:57Z</dc:date>
    <item>
      <title>How do I unzip a file when pulling it from REST API?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328651#M60981</link>
      <description>&lt;P&gt;So the rest API that I set up in Splunk will go out to this rest endpoint and the file that it will receive is a zip file. Inside this zip file, there are 2 CSV files but I only need to index 1 file (in this case, the file name is ENDPOINT_CDR_DETAIL_ALL_CSV). But I only see 3 options for the response type which is text, xml, and json. Does Splunk have an option for us to set may be a response handler to unzip the file and only index 1 file out of the 2?&lt;/P&gt;

&lt;P&gt;The name and form of the file:&lt;BR /&gt;
&lt;IMG src="https://community.splunk.com/storage/temp/218620-zipfilename.png" alt="alt text" /&gt;&lt;/P&gt;

&lt;P&gt;Content inside the zip file:&lt;BR /&gt;
&lt;IMG src="https://community.splunk.com/storage/temp/218621-contentinzipfile.png" alt="alt text" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 16:26:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328651#M60981</guid>
      <dc:creator>tamduong16</dc:creator>
      <dc:date>2020-09-29T16:26:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I unzip a file when pulling it from REST API?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328652#M60982</link>
      <description>&lt;P&gt;Or if REST API couldn't do this. Is there any alternative way?&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 16:40:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328652#M60982</guid>
      <dc:creator>tamduong16</dc:creator>
      <dc:date>2017-10-27T16:40:18Z</dc:date>
    </item>
    <item>
      <title>Re: How do I unzip a file when pulling it from REST API?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328653#M60983</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;can you please let me how you call REST API, using the script or anything else ??&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 16:50:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328653#M60983</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2017-10-27T16:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: How do I unzip a file when pulling it from REST API?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328654#M60984</link>
      <description>&lt;P&gt;I was able to download the rest api from splunk but for now, I'm not using any script yet. Do you think I could do this by writing a script that could run every minute to go to the url api? Again if the script allows me to unzip the file and pick what file I want. Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 17:17:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328654#M60984</guid>
      <dc:creator>tamduong16</dc:creator>
      <dc:date>2017-10-27T17:17:27Z</dc:date>
    </item>
    <item>
      <title>Re: How do I unzip a file when pulling it from REST API?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328655#M60985</link>
      <description>&lt;P&gt;I suggest using the &lt;A href="https://splunkbase.splunk.com/app/1546/"&gt;REST API Modualr Input&lt;/A&gt; and plugging in a custom response handler to perform the unzipping for you and any other pre processing you require.&lt;/P&gt;

&lt;P&gt;&lt;A href="https://answers.splunk.com/answers/575554/rest-api-option-for-compressed-file-can-i-index-a.html"&gt;Here is an example in another answer.&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 19:52:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328655#M60985</guid>
      <dc:creator>Damien_Dallimor</dc:creator>
      <dc:date>2017-10-27T19:52:38Z</dc:date>
    </item>
    <item>
      <title>Re: How do I unzip a file when pulling it from REST API?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328656#M60986</link>
      <description>&lt;P&gt;Could you give me more information as how do I make the handler give the specific file to the indexer&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 20:58:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328656#M60986</guid>
      <dc:creator>tamduong16</dc:creator>
      <dc:date>2017-10-27T20:58:20Z</dc:date>
    </item>
    <item>
      <title>Re: How do I unzip a file when pulling it from REST API?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328657#M60987</link>
      <description>&lt;P&gt;In &lt;CODE&gt;rest_ta/bin/responsehandlers.py&lt;/CODE&gt; add a custom response handler , pseudo example : &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;class ZipFileResponseHandler:

def __init__(self,**args):
    self.csv_file_to_index = args['csv_file_to_index']

def __call__(self, response_object,raw_response_output,response_type,req_args,endpoint):
    import zipfile,io,re
    file = zipfile.ZipFile(BytesIO(response_object.content))
    for info in file.infolist():
        if re.match(self.csv_file_to_index, info.filename):
            filecontent = file.read(info)
            print_xml_stream(filecontent)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In your config stanza , apply this handler :&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/3718iCD676C6608BF8B18/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;The &lt;CODE&gt;csv_file_to_index&lt;/CODE&gt; parameter value in this example is a &lt;STRONG&gt;python regex&lt;/STRONG&gt; such as :&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;CODE&gt;ENDPOINT_CDR_DETAIL_ALL_CSV\.csv&lt;/CODE&gt;for an exact filename to extract from the zip&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;CODE&gt;.*CDR_DETAIL.*\.csv$&lt;/CODE&gt;  for a pattern for the filename(s) to extract from the zip&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Sat, 28 Oct 2017 04:07:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328657#M60987</guid>
      <dc:creator>Damien_Dallimor</dc:creator>
      <dc:date>2017-10-28T04:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I unzip a file when pulling it from REST API?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328658#M60988</link>
      <description>&lt;P&gt;Yes,&lt;BR /&gt;
you can create scripted input which downloads and extracts files for you. &lt;/P&gt;

&lt;P&gt;Create inputs.conf in your app and put below configuration in file.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[script:///opt/splunk/etc/app/yourapp/bin/scriptedfile.py]
disabled = 0
interval = 60
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This will run file every 60 secs. You can change as per your requirement.&lt;/P&gt;

&lt;P&gt;Create &lt;CODE&gt;bin/scriptedfile.py&lt;/CODE&gt; and do code for REST API (file download ) and extraction of files.&lt;/P&gt;

&lt;P&gt;Scripted Input docs:&lt;BR /&gt;
&lt;A href="https://docs.splunk.com/Documentation/SplunkCloud/6.6.3/AdvancedDev/ScriptedInputsIntro"&gt;https://docs.splunk.com/Documentation/SplunkCloud/6.6.3/AdvancedDev/ScriptedInputsIntro&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2017 05:42:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328658#M60988</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2017-10-30T05:42:44Z</dc:date>
    </item>
    <item>
      <title>Re: How do I unzip a file when pulling it from REST API?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328659#M60989</link>
      <description>&lt;P&gt;This is my version of the code:&lt;/P&gt;

&lt;P&gt;class ZipFileResponseHandler:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def __init__(self,**args):
    pass

def __call__(self, response_object, raw_response_output, response_type, req_args, endpoint):
    file = zipfile.ZipFile(StringIO.StringIO(response_object.content))
    for name in file.namelist():
        if "ENDPOINT" in name:
            data =file.read(name)
            data = data.split('\n')
            for element in data[1:]:
                print_xml_stream(element)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Nov 2017 21:28:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-I-unzip-a-file-when-pulling-it-from-REST-API/m-p/328659#M60989</guid>
      <dc:creator>tamduong16</dc:creator>
      <dc:date>2017-11-14T21:28:57Z</dc:date>
    </item>
  </channel>
</rss>

