<?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 Splunk : Send File to python endpoint in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Splunk-Send-File-to-python-endpoint/m-p/394856#M6683</link>
    <description>&lt;P&gt;Hello, &lt;/P&gt;

&lt;P&gt;I'm facing a problem, I am trying to send a file from a custom form to a python endpoint but I can't get my file , Any example of how to do it or any help please, I have tried with formdata but It seems that this formData is lost in request , And when I post with an object, My Python script gets it as a string so I can't do anything with it.&lt;BR /&gt;
Here's the code : &lt;BR /&gt;
&lt;STRONG&gt;Javascript :&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;
var service = mvc.createService();&lt;BR /&gt;
        var data = $("#userPostForm").serializeArray();&lt;BR /&gt;
        var data_obj = {};&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    var fdata = new FormData()
    /**
     *  This part FOR FILE :*/
    var file = $('#myFile').prop('files');
    if (file.length != 0) {
        console.log('file :', file)
        fdata.append('file',file)
        data_obj['file']=file
    } else {
        $('#postResponseBox').empty();
        $('#postResponseBox').append('&amp;lt;p&amp;gt;Vous devez télécharger un fichier&amp;lt;/p&amp;gt;');
        setTimeout(function () { $('#postResponseBox').empty(); }, 3000);
    }



    _.each(data, function (field) {
        var key = field['name'];
        var value = field['value'];
        fdata.append(key,value);
        data_obj[key]=value;

    })
    //  console.log('form data : '+ JSON.stringify(fdata));
    // Display the key/value pairs
    for (var pair of fdata.entries()) {
        console.log(pair[0]+ ', ' + pair[1]); 
    }

    service.post('/services/sendfile', fdata, function (err, response) {

        if (err) {
            console.log('error  :', err);
            $('#postResponseBox').empty();
            $('#postResponseBox').append('&amp;lt;p&amp;gt;' + err.data + '&amp;lt;/p&amp;gt;');
            $('#userPostForm')[0].reset();
            setTimeout(function () { $('#postResponseBox').empty(); }, 3000);
        }
        else if (response.status === 200) {
            console.log('response from backend :', response);
            $('#postResponseBox').empty();
            $('#postResponseBox').append('&amp;lt;p class="successBox"&amp;gt;Username Submited successfuly!&amp;lt;/p&amp;gt;');
            $('#userPostForm')[0].reset();
            setTimeout(function () { $('#postResponseBox').empty(); }, 3000);
        }

    })
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Python&lt;/STRONG&gt;&lt;BR /&gt;
import splunk&lt;BR /&gt;
from splunk.rest import logger&lt;BR /&gt;
import requests&lt;/P&gt;

&lt;P&gt;class Send(splunk.rest.BaseRestHandler):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def handle_POST(self):

    try:
        payload = self.request["payload"]
        for el in payload.split("&amp;amp;"):
            key, value = el.split("=")  # type: (object, object)

            if 'file' in key:
                file = value
                logger.info(type(file)) # This is detected as string 

            #  self.process_csv(value)


    except Exception, e:
        self.response.write(e)
        logger.error(e)

handle_GET = handle_POST
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Please Any Help.&lt;/P&gt;</description>
    <pubDate>Wed, 09 Jan 2019 18:13:36 GMT</pubDate>
    <dc:creator>virtuosoo</dc:creator>
    <dc:date>2019-01-09T18:13:36Z</dc:date>
    <item>
      <title>Splunk : Send File to python endpoint</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Splunk-Send-File-to-python-endpoint/m-p/394856#M6683</link>
      <description>&lt;P&gt;Hello, &lt;/P&gt;

&lt;P&gt;I'm facing a problem, I am trying to send a file from a custom form to a python endpoint but I can't get my file , Any example of how to do it or any help please, I have tried with formdata but It seems that this formData is lost in request , And when I post with an object, My Python script gets it as a string so I can't do anything with it.&lt;BR /&gt;
Here's the code : &lt;BR /&gt;
&lt;STRONG&gt;Javascript :&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;
var service = mvc.createService();&lt;BR /&gt;
        var data = $("#userPostForm").serializeArray();&lt;BR /&gt;
        var data_obj = {};&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    var fdata = new FormData()
    /**
     *  This part FOR FILE :*/
    var file = $('#myFile').prop('files');
    if (file.length != 0) {
        console.log('file :', file)
        fdata.append('file',file)
        data_obj['file']=file
    } else {
        $('#postResponseBox').empty();
        $('#postResponseBox').append('&amp;lt;p&amp;gt;Vous devez télécharger un fichier&amp;lt;/p&amp;gt;');
        setTimeout(function () { $('#postResponseBox').empty(); }, 3000);
    }



    _.each(data, function (field) {
        var key = field['name'];
        var value = field['value'];
        fdata.append(key,value);
        data_obj[key]=value;

    })
    //  console.log('form data : '+ JSON.stringify(fdata));
    // Display the key/value pairs
    for (var pair of fdata.entries()) {
        console.log(pair[0]+ ', ' + pair[1]); 
    }

    service.post('/services/sendfile', fdata, function (err, response) {

        if (err) {
            console.log('error  :', err);
            $('#postResponseBox').empty();
            $('#postResponseBox').append('&amp;lt;p&amp;gt;' + err.data + '&amp;lt;/p&amp;gt;');
            $('#userPostForm')[0].reset();
            setTimeout(function () { $('#postResponseBox').empty(); }, 3000);
        }
        else if (response.status === 200) {
            console.log('response from backend :', response);
            $('#postResponseBox').empty();
            $('#postResponseBox').append('&amp;lt;p class="successBox"&amp;gt;Username Submited successfuly!&amp;lt;/p&amp;gt;');
            $('#userPostForm')[0].reset();
            setTimeout(function () { $('#postResponseBox').empty(); }, 3000);
        }

    })
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Python&lt;/STRONG&gt;&lt;BR /&gt;
import splunk&lt;BR /&gt;
from splunk.rest import logger&lt;BR /&gt;
import requests&lt;/P&gt;

&lt;P&gt;class Send(splunk.rest.BaseRestHandler):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def handle_POST(self):

    try:
        payload = self.request["payload"]
        for el in payload.split("&amp;amp;"):
            key, value = el.split("=")  # type: (object, object)

            if 'file' in key:
                file = value
                logger.info(type(file)) # This is detected as string 

            #  self.process_csv(value)


    except Exception, e:
        self.response.write(e)
        logger.error(e)

handle_GET = handle_POST
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Please Any Help.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 18:13:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Splunk-Send-File-to-python-endpoint/m-p/394856#M6683</guid>
      <dc:creator>virtuosoo</dc:creator>
      <dc:date>2019-01-09T18:13:36Z</dc:date>
    </item>
  </channel>
</rss>

