Splunk Dev

Splunk : Send File to python endpoint

virtuosoo
Explorer

Hello,

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.
Here's the code :
Javascript :

var service = mvc.createService();
var data = $("#userPostForm").serializeArray();
var data_obj = {};

    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('<p>Vous devez télécharger un fichier</p>');
        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('<p>' + err.data + '</p>');
            $('#userPostForm')[0].reset();
            setTimeout(function () { $('#postResponseBox').empty(); }, 3000);
        }
        else if (response.status === 200) {
            console.log('response from backend :', response);
            $('#postResponseBox').empty();
            $('#postResponseBox').append('<p class="successBox">Username Submited successfuly!</p>');
            $('#userPostForm')[0].reset();
            setTimeout(function () { $('#postResponseBox').empty(); }, 3000);
        }

    })

Python
import splunk
from splunk.rest import logger
import requests

class Send(splunk.rest.BaseRestHandler):

def handle_POST(self):

    try:
        payload = self.request["payload"]
        for el in payload.split("&"):
            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

Please Any Help.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...