How to post a file to url without saving the data to disk?

51 Views Asked by At

Note: I am running the following code in Nashorn. I have a file downloaded by the application and the raw content stored in a variable encoded in base64, and I want to send this over http in a multipart form.

I tried many things but it looks like I am missing something in the way I am sending bytes over.

//some code before to get the bearer token, no issue with that
var boundary = java.util.UUID.randomUUID().toString();
var headers = {
   "Authorization": "Bearer "+ token,
   "Transfer-Encoding": "chunked",
   "Content-Type": "multipart/form-data; boundary=" + boundary
};
var con = new java.net.URL("theUrl").openConnection();
con.requestMethod = "POST";
var hk = Object.keys(headers);
for (var key of hk){
    con.setRequestProperty(key, headers[key]);
}
// Send post request
con.doOutput=true;
var rn="\r\n";
var dos = new java.io.DataOutputStream(con.outputStream);
var b64 = Java.type("java.util.Base64");
dos.writeBytes("--" + boundary + rn);
dos.writeBytes("Content-Disposition: form-data; name=\"mycontent\"; filename=\"mycontent.zip\"" + rn);
dos.writeBytes("Content-Type: application/octet-stream" + rn);
dos.writeBytes("Content-Transfer-Encoding: binary" + rn);
dos.writeBytes(rn);
mydata = b64.getDecoder().decode(content_pack).toString().getBytes(); //here is probably my problem
dos.write(mydata, 0, mydata.length); //or maybe here?
dos.writeBytes(rn); 
dos.flush(); 
// End of multipart/form-data.
dos.writeBytes("--" + boundary + "--" + rn);
dos.flush();
dos.close();    

asResponse(con); //just read the response and return a json object

The above sent to httpbin.org would result as the following :

{
"data": "{  \"args\": {},   \"data\": \"\",   \"files\": {    \"content-pack\": \"[B@5e3b67a5\"  },   \"form\": {},   \"headers\": {    \"Accept\": \"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\",     \"Authorization\": \"Bearer Dab35ZB60lIdgI\",     \"Content-Length\": \"254\",     \"Content-Type\": \"multipart/form-data;boundary=b9aff414-4066-4e4b-9165-fc315ca4e02e\",     \"Host\": \"httpbin.org\",     \"User-Agent\": \"Java/11.0.20\",     \"X-Amzn-Trace-Id\": \"Root=1-64e5cbc3-3feefbcc4bcb86c374402dc8\"  },   \"json\": null,   \"origin\": \"111.1.26.12\",   \"url\": \"https://httpbin.org/post\"}",
"headers": {
    "null": "HTTP/1.1 200 OK",
    "Server": "gunicorn/19.9.0",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Credentials": "true",
    "Connection": "keep-alive",
    "Content-Length": "587",
    "Date": "Wed, 23 Aug 2023 09:05:08 GMT",
    "Content-Type": "application/json"
},
"statusCode": 200}

And sent to the real server that would just give me a 500 error with no explanation. Now, I cant use ByteArrayBody because the class is not found on the system the code should run, so I need to stick to the bare minimum.

If would appreciate very much if someone could help and let me know what I am doing wrong. Thanks a lot.

=== Example of base64 encoded zip file

UEsDBBQAAAAAAFyVF1cAAAAAAAAAAAAAAAAJACAAY29udGVudDEvVVQNAAeh1OVkp9TlZK3U5WR1eAsAAQS2FQQ2BFtqZANQSwMEFAAIAAgAS5UXVwAAAAAAAAAABBgAABIAIABjb250ZW50MS8uRFNfU3RvcmVVVA0AB3/U5WSd1OVkf9TlZHV4CwABBLYVBDYEW2pkA+2YOw7CMBBEZ40LSzQuKd1wAG5gRckJuAAFV6D30SHaEbIUUlAlgnmS9VaKf2kcTwDY8LhfgAwgwY0zPpLYFoSuNs4hhBBCiH1jrnTcdhtCiB0ynw+FrnRzG58HOnZjMl3oSje3sV+gI53oTBe60s3NQ8sYPowrGxOKMYVYoetXryzE33Bw5fn7P2E1/wshfhiL43Uc8A4Eyw6vduvqhvVLQPCfhadubKEr3dy6CAixFU9QSwcIagCIbbIAAAAEGAAAUEsDBBQACAAIAEuVF1cAAAAAAAAAAHgAAAAdACAAX19NQUNPU1gvY29udGVudDEvLl8uRFNfU3RvcmVVVA0AB3/U5WSd1OVksdTlZHV4CwABBLYVBDYEW2pkA2NgFWNnYGJg8E1MVvAPVohQgAKQGAMnEBsBsRsQg/gVQMwAU+EgwIADOIaEBEGZFTBd6AAAUEsHCAuIwDg1AAAAeAAAAFBLAwQUAAgACABclRdXAAAAAAAAAAASAAAAGwAgAGNvbnRlbnQxL0EgZGF0YSBmaWxlIHppcHBlZFV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