POST multipart/form-data with PDF as body

Forum to discuss the Web client version of Servoy.

POST multipart/form-data with PDF as body

Postby jeffrey.vandenhondel » Wed Jul 26, 2017 11:05 am

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
jeffrey.vandenhondel
 
Posts: 32
Joined: Thu Jul 28, 2016 9:05 am

Re: POST multipart/form-data with PDF as body

Postby vschuurhof » Mon Sep 18, 2017 9:09 am

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)
Vincent Schuurhof
Servoy
vschuurhof
 
Posts: 69
Joined: Tue Dec 14, 2010 12:00 pm

Re: POST multipart/form-data with PDF as body

Postby jeffrey.vandenhondel » Tue Oct 31, 2017 2:33 pm

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)


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.

Code: Select all
   /**
    * @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;
   }
jeffrey.vandenhondel
 
Posts: 32
Joined: Thu Jul 28, 2016 9:05 am


Return to Servoy Web Client

Who is online

Users browsing this forum: No registered users and 3 guests