I have a curl command that I want to adapt to javascript using ajax.
curl -v -X 'POST' --data-binary @BinaryData.bin.txt "http://127.0.0.1:3000/api/v1/update_data"
In javascript I used FileReader() and read the file as Text, BinaryString, Array Buffer with different ajax params settings for processData, contentType, cache, etc several times but did not succeed in sending the proper binary string like in python example below.
I tried doing it in python and the following code seems to work as intended:
import requests
import os
path = os.path.normpath('d:/BinaryData.bin.txt')
file = open(path, 'rb')
data = file.read()
r = requests.post("http://127.0.0.1:3000/api/v1/update_data", data=data)
What am I missing in Javascript that it doesn't seem to send the correct data from this file?
Example of how I tried doing it in javascript:
onFileSelected: function(evt) {
var file = evt.target.files[0];
var reader = new FileReader();
reader.onload = (function (file) {
return function(e) {
var data = e.target.result;
$.ajax({
url: "http://127.0.0.1:3000/api/v1/update_data",
data: data,
contentType: 'application/octet-stream',
processData: false,
type: "POST",
success: function () {
// all good
},
error: function() {
// failed
}
});
}
reader.readAsBinaryString(file);
}