<?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: Return binary data (such as images) via REST API or otherwise in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219182#M43056</link>
    <description>&lt;P&gt;OK, this exercise is done. Here are my steps:&lt;BR /&gt;
 - Create an application named test_rest_api.&lt;BR /&gt;
 - Create the following stanza in $SPLUNK_HOME/etc/system/local/web.conf:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; [expose:test_rest_api]
 methods = GET
 pattern = test_rest_api/test
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;UL&gt;
&lt;LI&gt;&lt;P&gt;Create the following stanza in $SPLUNK_HOME/etc/system/local/restmap.conf:&lt;/P&gt;

&lt;P&gt;[script:testRestApi]&lt;BR /&gt;
 match = /test_rest_api/test&lt;BR /&gt;
 scripttype = python&lt;BR /&gt;
 handler = test_rest_script.MyImageHandler&lt;BR /&gt;
 requireAuthentication = false&lt;/P&gt;&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;Create a python script named test_rest_script.py in etc/apps/test_rest_api/bin folder). Here is the entire script:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunk
import splunk.rest as rest
import splunk.entity as entity
import splunk.auth
import splunk.models.dashboard as sm_dashboard
import splunk.models.dashboard_panel as sm_dashboard_panel
import splunk.models.saved_search as sm_saved_search
import splunk.search
import splunk.search.searchUtils
from splunk.util import normalizeBoolean

import os, sys

class MyImageHandler(splunk.rest.BaseRestHandler):
    TEST_API_BINARY_MODE = 'binary'
    BINARY_FILE_NAME = '../etc/apps/test_rest_api/appserver/static/rubber-duck.jpg' # relative to $SPLUNK_HOME/bin
    message = 'Test &amp;lt;strong&amp;gt;REST&amp;lt;/strong&amp;gt; API'
    status = 200

    def handle_GET(self):
        if self.TEST_API_BINARY_MODE in self.args and self.args.get(self.TEST_API_BINARY_MODE) in  ('true', 'True', 'y', 'yes', '1'):
            self._binResponse()
        else:
            self._textResponse()

    def _textResponse(self):
        self.response.write(self.message)
        self.response.setStatus(self.status)
        self.response.setHeader("content-type", "text/html")

    def _binResponse(self):
        try:
            data = self._getImage()
            self.response.write(data)
            self.response.setStatus(self.status)
            self.response.setHeader('content-type','image/jpg')
        except Exception as e:
            self.message = str(e)
            self.status = 501
            self._textResponse()

    def _getFileName(self):
        scriptname = os.path.abspath(sys.argv[0])
        scriptdir = os.path.dirname(scriptname)
        imgname = os.path.normpath('/'.join([ scriptdir, self.BINARY_FILE_NAME ]))
        return imgname

    def _getImage(self):
        imgname = self._getFileName()
        f = open(imgname, 'rb')
        data = f.read()
        f.close()
        return data
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Why is sys.argv[0] returning a $SPLUNK_HOME/bin folder rather than etc/apps/test_rest_api/bin, I don't know, but it's used for testing purposes only (I've put that rubber-duck.jpg in my app's appserver/static). My real script will read the image from an http link :).&lt;/P&gt;

&lt;P&gt;So you just enter &lt;A href="https://localhost:8089/services/test_rest_api/test?binary=true" target="_blank"&gt;https://localhost:8089/services/test_rest_api/test?binary=true&lt;/A&gt; and enjoy the picture of the rubber duck! Or omit that binary parameter (or, say, put 'false' there) and look at the proud 'Test &lt;STRONG&gt;REST&lt;/STRONG&gt; API' message.&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 07:17:46 GMT</pubDate>
    <dc:creator>arkadyz1</dc:creator>
    <dc:date>2020-09-29T07:17:46Z</dc:date>
    <item>
      <title>Return binary data (such as images) via REST API or otherwise</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219175#M43049</link>
      <description>&lt;P&gt;I'm trying to make some custom extensions to our application, with some additional html divs displaying images. The application will get those images from an external image server and generate some URL to serve as src= in img HTML tag. As a possibility, I can envision our python script (invoked via REST API?) storing images somewhere under appserver/static and generating the proper URLs to access them.&lt;/P&gt;

&lt;P&gt;Are there any ready examples demonstrating something like that? I can try to use &lt;CODE&gt;pdfgen_endpoint.py&lt;/CODE&gt; as an example, but that might take a while...&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2015 20:36:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219175#M43049</guid>
      <dc:creator>arkadyz1</dc:creator>
      <dc:date>2015-09-10T20:36:52Z</dc:date>
    </item>
    <item>
      <title>Re: Return binary data (such as images) via REST API or otherwise</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219176#M43050</link>
      <description>&lt;P&gt;Are you asking how to create your own splunk rest endpoint?&lt;/P&gt;</description>
      <pubDate>Fri, 11 Sep 2015 21:54:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219176#M43050</guid>
      <dc:creator>bmacias84</dc:creator>
      <dc:date>2015-09-11T21:54:37Z</dc:date>
    </item>
    <item>
      <title>Re: Return binary data (such as images) via REST API or otherwise</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219177#M43051</link>
      <description>&lt;P&gt;In fact, both - how to create a custom endpoint &lt;EM&gt;and&lt;/EM&gt; how to handle that in python. However, I did look deeper into pdfgen since I've sent this question, so now I have some crude idea. So far I see the following:&lt;BR /&gt;
1) web.conf: [expose:uniqueName] stanza with pattern=... and methods=... at least&lt;BR /&gt;
2) restmap.conf: [script:description] stanza with match=..., handler=... and requiresAuthentication=...&lt;BR /&gt;
3) The Python script: a class matching the class name in handler in restmap.conf, with handle_GET etc. methods. The method returning a binary file would read it into a byte array, write that into the response, set the HTTP header content-type to application/octet-stream or similar and return.&lt;/P&gt;

&lt;P&gt;Any pitfalls on this way?&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2015 15:55:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219177#M43051</guid>
      <dc:creator>arkadyz1</dc:creator>
      <dc:date>2015-09-14T15:55:30Z</dc:date>
    </item>
    <item>
      <title>Re: Return binary data (such as images) via REST API or otherwise</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219178#M43052</link>
      <description>&lt;P&gt;Your heading down the right path.  Here is another good example of an app using the restmap.  &lt;A href="https://splunkbase.splunk.com/app/1607/"&gt;https://splunkbase.splunk.com/app/1607/&lt;/A&gt;.  You should join the IRC chat, lots of the experts hang out there.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2015 16:20:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219178#M43052</guid>
      <dc:creator>bmacias84</dc:creator>
      <dc:date>2015-09-14T16:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: Return binary data (such as images) via REST API or otherwise</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219179#M43053</link>
      <description>&lt;P&gt;Status update:&lt;/P&gt;

&lt;P&gt;So far, I learned how to create an endpoint and tie it to a script. I did the following:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;Created an application with no views (Visible: false) named test_rest_api (very creative name, I know).&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Added an [expose:...] stanza to web.conf (if you don't have one in etc/system/local, create it - just don't modify the one in etc/system/default!). The stanza looked like this:&lt;/P&gt;

&lt;P&gt;[expose:test_rest_api]&lt;BR /&gt;
methods = GET&lt;BR /&gt;
pattern = test_rest_api/test&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Added a [script:...] stanza to the restmap.conf in etc/system/local. It looked like this:&lt;/P&gt;

&lt;P&gt;[script:testRestApi]&lt;BR /&gt;
match = /test_rest_api/test&lt;BR /&gt;
scripttype = python&lt;BR /&gt;
handler = test_rest_script.MyImageHandler&lt;BR /&gt;
requireAuthentication = false&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Created a script named test_rest_script.py (very creative again) in etc/apps/test_rest_api/bin. The file looks like this so far:&lt;/P&gt;

&lt;P&gt;import splunk&lt;BR /&gt;
import splunk.rest as rest&lt;BR /&gt;
import splunk.entity as entity&lt;BR /&gt;
import splunk.auth&lt;BR /&gt;
import splunk.models.dashboard as sm_dashboard&lt;BR /&gt;
import splunk.models.dashboard_panel as sm_dashboard_panel&lt;BR /&gt;
import splunk.models.saved_search as sm_saved_search&lt;BR /&gt;
import splunk.search&lt;BR /&gt;
import splunk.search.searchUtils&lt;BR /&gt;
from splunk.util import normalizeBoolean&lt;/P&gt;

&lt;P&gt;class MyImageHandler(splunk.rest.BaseRestHandler):&lt;BR /&gt;
    TEST_API_BINARY_MODE = 'binary'&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def handle_GET(self):
    self._textResponse()

def _textResponse(self):
    self.response.write("Test &amp;lt;strong&amp;gt;REST&amp;lt;/strong&amp;gt; API")
    self.response.setStatus(200)
    self.response.setHeader("content-type", "text/html")
&lt;/CODE&gt;&lt;/PRE&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;I'm not sure I'll need all those imports - I mimicked &lt;CODE&gt;pdfgen_endpoint.py&lt;/CODE&gt; in this. Anyway, my next step would be to put an image file somewhere within my app (probably in appserver/static, though not necessary), read that if the requested mode is binary and write it into the response. Will update once I succeed. Right now I do get that Test &lt;STRONG&gt;REST&lt;/STRONG&gt; API on the screen when I enter &lt;A href="https://localhost:8089/services/test_rest_api/test/" target="_blank"&gt;https://localhost:8089/services/test_rest_api/test/&lt;/A&gt; - with or without the trailing slash in the URL.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 07:15:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219179#M43053</guid>
      <dc:creator>arkadyz1</dc:creator>
      <dc:date>2020-09-29T07:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: Return binary data (such as images) via REST API or otherwise</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219180#M43054</link>
      <description>&lt;P&gt;Another update - I got it all working. All that I still had to do was this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;        f = open(imgname, 'rb')
        data = f.read()
        f.close()
        self.response.write(data)
        self.response.setStatus(self.status)
        self.response.setHeader('content-type','image/jpg')
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here imgname was the file name of the image I wanted to show. I used &lt;CODE&gt;content-type&lt;/CODE&gt; of &lt;CODE&gt;image/jpg&lt;/CODE&gt; rather than &lt;CODE&gt;application/octet-stream&lt;/CODE&gt; only because I tested that in Chrome and it does not show such content as image, instead offering to download it.&lt;BR /&gt;
Anyway, this concludes this exercise. I guess in the end I answered my own question - again &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2015 21:24:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219180#M43054</guid>
      <dc:creator>arkadyz1</dc:creator>
      <dc:date>2015-09-16T21:24:59Z</dc:date>
    </item>
    <item>
      <title>Re: Return binary data (such as images) via REST API or otherwise</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219181#M43055</link>
      <description>&lt;P&gt;Glad you are figuring things out.  Here some points.  When your finish I recommend posting answer to the question with you final result.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2015 21:47:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219181#M43055</guid>
      <dc:creator>bmacias84</dc:creator>
      <dc:date>2015-09-16T21:47:57Z</dc:date>
    </item>
    <item>
      <title>Re: Return binary data (such as images) via REST API or otherwise</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219182#M43056</link>
      <description>&lt;P&gt;OK, this exercise is done. Here are my steps:&lt;BR /&gt;
 - Create an application named test_rest_api.&lt;BR /&gt;
 - Create the following stanza in $SPLUNK_HOME/etc/system/local/web.conf:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; [expose:test_rest_api]
 methods = GET
 pattern = test_rest_api/test
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;UL&gt;
&lt;LI&gt;&lt;P&gt;Create the following stanza in $SPLUNK_HOME/etc/system/local/restmap.conf:&lt;/P&gt;

&lt;P&gt;[script:testRestApi]&lt;BR /&gt;
 match = /test_rest_api/test&lt;BR /&gt;
 scripttype = python&lt;BR /&gt;
 handler = test_rest_script.MyImageHandler&lt;BR /&gt;
 requireAuthentication = false&lt;/P&gt;&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;Create a python script named test_rest_script.py in etc/apps/test_rest_api/bin folder). Here is the entire script:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunk
import splunk.rest as rest
import splunk.entity as entity
import splunk.auth
import splunk.models.dashboard as sm_dashboard
import splunk.models.dashboard_panel as sm_dashboard_panel
import splunk.models.saved_search as sm_saved_search
import splunk.search
import splunk.search.searchUtils
from splunk.util import normalizeBoolean

import os, sys

class MyImageHandler(splunk.rest.BaseRestHandler):
    TEST_API_BINARY_MODE = 'binary'
    BINARY_FILE_NAME = '../etc/apps/test_rest_api/appserver/static/rubber-duck.jpg' # relative to $SPLUNK_HOME/bin
    message = 'Test &amp;lt;strong&amp;gt;REST&amp;lt;/strong&amp;gt; API'
    status = 200

    def handle_GET(self):
        if self.TEST_API_BINARY_MODE in self.args and self.args.get(self.TEST_API_BINARY_MODE) in  ('true', 'True', 'y', 'yes', '1'):
            self._binResponse()
        else:
            self._textResponse()

    def _textResponse(self):
        self.response.write(self.message)
        self.response.setStatus(self.status)
        self.response.setHeader("content-type", "text/html")

    def _binResponse(self):
        try:
            data = self._getImage()
            self.response.write(data)
            self.response.setStatus(self.status)
            self.response.setHeader('content-type','image/jpg')
        except Exception as e:
            self.message = str(e)
            self.status = 501
            self._textResponse()

    def _getFileName(self):
        scriptname = os.path.abspath(sys.argv[0])
        scriptdir = os.path.dirname(scriptname)
        imgname = os.path.normpath('/'.join([ scriptdir, self.BINARY_FILE_NAME ]))
        return imgname

    def _getImage(self):
        imgname = self._getFileName()
        f = open(imgname, 'rb')
        data = f.read()
        f.close()
        return data
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Why is sys.argv[0] returning a $SPLUNK_HOME/bin folder rather than etc/apps/test_rest_api/bin, I don't know, but it's used for testing purposes only (I've put that rubber-duck.jpg in my app's appserver/static). My real script will read the image from an http link :).&lt;/P&gt;

&lt;P&gt;So you just enter &lt;A href="https://localhost:8089/services/test_rest_api/test?binary=true" target="_blank"&gt;https://localhost:8089/services/test_rest_api/test?binary=true&lt;/A&gt; and enjoy the picture of the rubber duck! Or omit that binary parameter (or, say, put 'false' there) and look at the proud 'Test &lt;STRONG&gt;REST&lt;/STRONG&gt; API' message.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 07:17:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219182#M43056</guid>
      <dc:creator>arkadyz1</dc:creator>
      <dc:date>2020-09-29T07:17:46Z</dc:date>
    </item>
    <item>
      <title>Re: Return binary data (such as images) via REST API or otherwise</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219183#M43057</link>
      <description>&lt;P&gt;I found out why sys.argv[0] has $SPLUNK_HOME/bin folder in the script name. The script being run is in fact $SPLUNK_HOME/bin/runSript.py, with the parameter of test_rest_script.MyImageHandler (script name.class name). Makes sense...&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 07:19:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Return-binary-data-such-as-images-via-REST-API-or-otherwise/m-p/219183#M43057</guid>
      <dc:creator>arkadyz1</dc:creator>
      <dc:date>2020-09-29T07:19:10Z</dc:date>
    </item>
  </channel>
</rss>

