1

So I can convert an image to base64, and then POST the image data with JSON, which is convenient, like so:

    curl -u "username:pwd" \
    -X PUT \
    -H "Content-Type: application/json" \
    -d '{"image":"my-base64-str-data"}' \
    http://maven.nabisco.com/artifactory/cdt-repo/folder/unique-image-id

However, my question is - is there a way to send the raw binary image data instead of encoding as base64? How could do that with either cURL or Node.js? Is it possible to send a file or binary data without using form-data in an HTTP request?

At the end of the day, however, I'd like to POST the image from the browser, and in that case, encoding the image as base64 might be the only way to go?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • Possible duplicate of [Binary Data in JSON String. Something better than Base64](https://stackoverflow.com/questions/1443158/binary-data-in-json-string-something-better-than-base64). See the answer from @ Ælex. – cybersam Jul 18 '17 at 23:20
  • no, I am not trying to put binary data in JSON, I am looking to write binary in the request, and avoid JSON altogether – Alexander Mills Jul 18 '17 at 23:24

1 Answers1

2

Curl

As you can read on the curl manpage, uploads of this form are done by specifying data strings, and can be done directly from a file with --data-binary @/path/to/file syntax:

  --data-binary <data>
          (HTTP) This posts data exactly as specified with no extra processing whatsoever.

          If you start the data with the letter @, the rest should be a filename.  Data is
          posted  in  a similar manner as --data-ascii does, except that newlines and car‐
          riage returns are preserved and conversions are never done.

          If this option is used several times, the ones following the first  will  append
          data as described in -d, --data.

If the image is only available as a binary string in your language, for example as a Node.js buffer, and you don't want to hit the filesystem, then you may have to escape it by enclosing it in ' characters and replacing every ' character inside of the string with an appropriate escape sequence like '\'' or, if that makes you uneasy, '"'"'. (Recall that echo 'abc'"def"'ghi' will echo abcdefghi as one single unit.)

Node.js

Node is a little more forgiving because it has an explicit buffer type, but it does require a bit more construction to make it work. Here I am wrapping the return data in a Promise in case you need it:

const http = require("http");
function upload(image_buffer, image_id) {
  return new Promise((accept, reject) => {
    let options = {
      method: "PUT",
      hostname: "maven.nabisco.com",
      port: 80,
      path: "/artifactory/cdt-repo/folder/" + image_id,
      headers: {
        "Content-Type": "application/octet-stream",
        "Content-Length": image_buffer.length
      }
    };
    let data = [];
    let request = http.request(options, response => {
      response.on("data", chunk => data.push(chunk));
      response.on("end", () =>
        accept({
          headers: response.headers,
          statusCode: response.statusCode,
          data: Buffer.concat(data)
        })
      );
    });
    request.on("error", err => reject(err));

    request.write(image_buffer);
    request.end();
  });
}
CR Drost
  • 9,637
  • 1
  • 25
  • 36
  • awesome I will try that - any idea how to send binary data from the browser to a server, without using form-data? that's my biggest question - so far I have only been able to use form-data with a base64 string, but I'd rather send image as raw binary from browser to Artifactory. – Alexander Mills Jul 19 '17 at 00:05
  • yeah using --data-binary worked - but like I said, I'd like to find a way to send binary data from the browser to a server. – Alexander Mills Jul 19 '17 at 00:11
  • 1
    @AlexanderMills Try [MDN - Web APIs - XMLHttpRequest - Sending and Receiving Binary Data](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data). – CR Drost Jul 19 '17 at 00:21
  • thanks, this is what I came up with (using the newer Fetch API for the browser) - https://stackoverflow.com/questions/45179058/post-binary-data-from-browser-to-server-without-using-form-data – Alexander Mills Jul 19 '17 at 00:25