# POST multipart/form-data with PDF as body

**URL:** <https://forum.servoy.com/t/post-multipart-form-data-with-pdf-as-body/19358>\
**Category:** Classic Servoy\
**Created:** [July 26, 2017, 9:05am UTC](https://forum.servoy.com/t/post-multipart-form-data-with-pdf-as-body/19358 "2017-07-26T09:05:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffrey.vandenhondel](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jeffrey.vandenhondel](https://forum.servoy.com/u/jeffrey.vandenhondel)\
**Post date:** [July 26, 2017, 9:05am UTC](https://forum.servoy.com/t/post-multipart-form-data-with-pdf-as-body/19358/1 "2017-07-26T09:05:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vschuurhof](https://avatars.discourse-cdn.com/v4/letter/v/46a35a/32.png) [@vschuurhof](https://forum.servoy.com/u/vschuurhof)\
**Post date:** [September 18, 2017, 7:09am UTC](https://forum.servoy.com/t/post-multipart-form-data-with-pdf-as-body/19358/2 "2017-09-18T07:09:13Z")

</div>

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](https://wiki.servoy.com/display/public/DOCS/PostRequest#PostRequest-addFile(parameterName,jsFile))

---

<div class="post-metadata">

**Author:** ![jeffrey.vandenhondel](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jeffrey.vandenhondel](https://forum.servoy.com/u/jeffrey.vandenhondel)\
**Post date:** [October 31, 2017, 12:33pm UTC](https://forum.servoy.com/t/post-multipart-form-data-with-pdf-as-body/19358/3 "2017-10-31T12:33:29Z")

</div>

> 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](https://wiki.servoy.com/display/public/DOCS/PostRequest#PostRequest-addFile(parameterName,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.

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

```

---

<div class="post-metadata">

**Author:** ![Andrei\_Costescu](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Andrei\_Costescu](https://forum.servoy.com/u/Andrei_Costescu)\
**Post date:** [February 12, 2021, 3:01pm UTC](https://forum.servoy.com/t/post-multipart-form-data-with-pdf-as-body/19358/4 "2021-02-12T15:01:49Z")

</div>

I bumped into this while looking at [https://support.servoy.com/browse/SVY-15850](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.
