1

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);
}
ADyson
  • 57,178
  • 14
  • 51
  • 63
alex1101
  • 31
  • 3
  • 1
    Like here maybe? https://stackoverflow.com/questions/19959072/sending-binary-data-in-javascript-over-http (also, you question should contain the code you've tried) –  Oct 24 '18 at 14:51
  • "What am I missing"...how can we know that, do you think? We can't fix your broken code if you don't show it to us. The other examples are of course useful as a comparison, but they don't reproduce the problem – ADyson Oct 24 '18 at 14:55
  • Added example of one of my failed approaches – alex1101 Oct 24 '18 at 15:04
  • and what exactly occurs when you run that? Did your HTTP request's body look as you expected? What response do you get from the server? – ADyson Oct 24 '18 at 15:37
  • Also your code seems to have some missing closing brackets. Wrapping the onload function in another function seems a bit of an overcomplication, anyhow. – ADyson Oct 24 '18 at 15:41
  • People seem to suggest just grabbing the raw `file` rather than using filereader, e.g. this answer https://stackoverflow.com/a/11399659/5947043 Might depend a bit exactly what the server is expecting, though – ADyson Oct 24 '18 at 15:46
  • I tried grabbing the the raw file but still doesn't work, missing closing brackets might be due to copy paste and some variable name changes... I guess that the server expects to get a binary string to then process it – alex1101 Oct 24 '18 at 16:04
  • And the errors I keep getting from the server look like this: Error: Uncaught error: Invalid type: -88 – alex1101 Oct 24 '18 at 16:27
  • And what http status code does it return? That kind of looks like a JS error rather than a http error. Did the request body look like you expected? – ADyson Oct 24 '18 at 17:12
  • Also have you considered using a tool like Fiddler to examine the HTTP request made by cURL and compare it to the one made by Ajax and see if there are differences we could address. – ADyson Oct 24 '18 at 17:16
  • I tried with fiddle and looking at the Raw data and also comparing it with WinDiff the difference is in the headers Python headers: `POST http://127.0.0.1:3000/api/v1/update_data HTTP/1.1 Host: 127.0.0.1:3000 User-Agent: python-requests/2.20.0 Accept-Encoding: gzip, deflate Accept: */* Connection: keep-alive Content-Length: 5410` – alex1101 Oct 25 '18 at 13:05
  • Javascript headers: `POST http://127.0.0.1:3000/api/v1/update_data HTTP/1.1 Host: 127.0.0.1:3000 Connection: keep-alive Content-Length: 5410 Pragma: no-cache Cache-Control: no-cache Origin: http://127.0.0.1:3000 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 Content-Type: application/x-www-form-urlencoded Accept: */* Referer: http://127.0.0.1:3000/ Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9` – alex1101 Oct 25 '18 at 13:06
  • The content type header in the JS version is a bit surprising, since you told Ajax to override that. Weird. Some people suggest setting contentType to 'false' when uploading files – ADyson Oct 30 '18 at 22:54

0 Answers0