Hi,
I am try to upload a file to a custom REST API endpoint, but I am not having much luck (I'm using Splunk 7.0.4). I have the following custom REST API handler which currently just echos back the request to the client browser (it is configured correctly and is being invoked):
import splunk
class FileUpload(splunk.rest.BaseRestHandler):
def handle_POST(self):
try:
self.response.write(str(self.request['payload']))
except Exception as e:
self.response.write("ERROR: " + str(e))
And I have some JavaScript which uses jQuery to perform an AJAX POST containing a selected file (I am testing using recent versions of Chrome and Firefox):
function uploadButtonCallback(e) {
e.preventDefault();
var fileInput = document.getElementById('appfile_id');
var file= fileInput.files[0];
var callback= function(response) {
console.log(response)
}
sendRequest('POST', URL, file, callback);
}
function sendRequest(type, url, file, callback) {
var formData = new FormData();
formData.append('file', file)
$.ajax({
'type': type,
'url' : url,
'timeout':30000,
'data': formData,
'processData': false,
'contentType': false
})
.success((response) => console.log(response))
.error((response) => console.log(response))
}
But I am not seeing the file content echoed back - this is what I get printed in the JS console:
18:41:44.517 Response: -----------------------------214284223518627931251367576440
Content-Disposition: form-data; name="file"; filename=file.pkg"
Content-Type: application/vnd.apple.installer+xml
I have been able to get this to work by using the JavaScript FileReader.readAsDataURL() function and sending the file as a base64 encoded String, however this does not work for large files (Chrome and Firefox both crash/fail).
I believe the client-side JavaScript is correct - it seems like something is stripping the binary data from the request before the Python handler is invoked. Can anyone help?
Thanks
PS if there is another way to have a file uploaded to a known location on the Splunk host (without it being indexed) - I would love to hear about it.
... View more