POST multipart/form-data with PDF as body

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

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)

vschuurhof:
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)

That didn’t work for me so i wrote the code myself if someone needs it here it is.

I did found the solution to my own question.

	/**
	 * @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;
	}

I bumped into this while looking at https://support.servoy.com/browse/SVY-15850

Before that case was implemented you could generate a multipart post by adding multiple files with addFile() or by having at least a file and a parameter (addParameter()).
Now there is a new method on Request that allows you to do single file multipart posts or param only multipart posts.

You can find more details in a comment of that case.