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!

Index This | What is broken 80% of the time by February?

December 2025 Edition   Hayyy Splunk Education Enthusiasts and the Eternally Curious!    We’re back with this ...

Unlock Faster Time-to-Value on Edge and Ingest Processor with New SPL2 Pipeline ...

Hello Splunk Community,   We're thrilled to share an exciting update that will help you manage your data more ...

Splunk MCP & Agentic AI: Machine Data Without Limits

Discover how the Splunk Model Context Protocol (MCP) Server can revolutionize the way your organization uses ...