Hello,
I try to send a POST request with the Content-Type of multipart/form-data.
For what i know is that i have to send the pdf as a body. is this possible with the plugins.http.
Kind regards,
Jeffrey
vschuurhof wrote:Yes, this is possible. The HTTP plugin has a function on the "PostRequest" object which is called "addFile". With this function you can attach a physical file from disk to the request and send it.
More information about that function can be found here: https://wiki.servoy.com/display/public/ ... ame,jsFile)
/**
* @author Jeffrey van den Hondel
* @since 30 aug. 2017 JvdH
*
* @param {String} url
* @param {plugins.file.JSFile} file
*
* @return {Boolean}
*/
this.postMultiPartPDF = function(url, file) {
var URL = url;
var obj = new java.net.URL(URL);
var connect = obj.openConnection();
var fileToUpload = new java.io.File(file.getAbsolutePath());
//Setting boundary settings
var boundary = "----123randomtext123";
var twoHyphens = "--";
var lineEnd = "\r\n";
//Set request header
connect.setRequestMethod("POST");
connect.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
connect.setDoOutput(true);
//Create ouput stream for the multipart/formdata.
var outputStream = new java.io.DataOutputStream(connect.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: application/pdf" + lineEnd);
outputStream.writeBytes(lineEnd);
var inputStreamToLogFile = new java.io.FileInputStream(fileToUpload);
var bytesRead;
var dataBuffer = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
while ( (bytesRead = inputStreamToLogFile.read(dataBuffer)) != -1) {
outputStream.write(dataBuffer, 0, bytesRead);
}
outputStream.writeBytes(twoHyphens + boundary + twoHyphens);
outputStream.writeBytes(lineEnd);
outputStream.flush();
outputStream.close();
var responseCode = connect.getResponseCode();
if (responseCode != 400) {
var input = new java.io.BufferedReader(new java.io.InputStreamReader(connect.getInputStream()));
var response;
//Read filled line
for (var line = input.readLine(); line != null; line = input.readLine()) {
response = (line);
}
input.close();
this.uploadResponse = response;
return true;
}
return false;
}
Users browsing this forum: No registered users and 12 guests