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!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...