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
Get Updates on the Splunk Community!

Splunk App for Anomaly Detection End of Life Announcement

Q: What is happening to the Splunk App for Anomaly Detection?A: Splunk is officially announcing the ...

Aligning Observability Costs with Business Value: Practical Strategies

 Join us for an engaging Tech Talk on Aligning Observability Costs with Business Value: Practical ...

Mastering Data Pipelines: Unlocking Value with Splunk

 In today's AI-driven world, organizations must balance the challenges of managing the explosion of data with ...