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();
});
}