CURL equivalent

Questions and answers on developing, deploying and using plugins and JavaBeans

CURL equivalent

Postby Roberto Blasco » Wed Jul 13, 2016 2:05 pm

Hi all.

How can I translate this curl command to Servoy with the http.plugin ?

Code: Select all
curl -X PUT --data-binary my_file.pdf http://localhost:8080 --header "Content-type: application/pdf"


or
Code: Select all
curl -T my_file.pdf http://localhost:8080 --header "Accept: text/plain"


Thanks in advance.

Best regards. Roberto Blasco
Un saludo. Roberto.

Madrid - Spain
Tfno: (+34) 625653066
E-mail: roberto.blasco.serrano@gmail.com
User avatar
Roberto Blasco
007
 
Posts: 355
Joined: Tue Apr 08, 2008 7:18 pm
Location: Madrid / Spain

Re: CURL equivalent

Postby Roberto Blasco » Wed Jul 13, 2016 10:24 pm

I am going to answer to myself :D

Code: Select all
/** @type {plugins.file.JSFile} */
var jsfile = plugins.file.createFile(filename_path);
   
var client = plugins.http.createNewHttpClient();
var request = client.createPutRequest("http://localhost:8080");
request.addHeader("Content-type","application/pdf");     // In this case is a pdf file
request.setFile(jsfile);
var response = request.executeRequest();


P.S.: Thanks to Adelo Herrero for his help !!!!
Un saludo. Roberto.

Madrid - Spain
Tfno: (+34) 625653066
E-mail: roberto.blasco.serrano@gmail.com
User avatar
Roberto Blasco
007
 
Posts: 355
Joined: Tue Apr 08, 2008 7:18 pm
Location: Madrid / Spain

Re: CURL equivalent

Postby fmhuertas » Wed Mar 08, 2017 9:15 pm

Hi all.

How can I translate this curl command to Servoy with the plugins.http?
Code: Select all
curl --insecure --progress-bar -X POST -F 'number=9999999999' -F 'text=texto prueba' https://IP:44443/EnvioSMS.php


Best regards. Fernando Huertas.
fmhuertas
 
Posts: 1
Joined: Wed Mar 08, 2017 6:12 pm

Re: CURL equivalent

Postby juan.cristobo » Thu Mar 09, 2017 12:14 pm

I'm not sure what -F option is, but have you tried this?

Code: Select all
var client = plugins.http.createNewHttpClient();
var request = client.createPostRequest("https://IP:44443/EnvioSMS.php");
request.addHeader("Content-type","multipart/form-data");
request.addParameter('number', '9999999999');
request.addParameter('text', 'texto prueba');
var response = request.executeRequest();
Juan
Madrid (Spain)

Servoy 7.4.x - MySQL / SQL Server 2008-2016
Windows 10 Pro
juan.cristobo
 
Posts: 186
Joined: Thu Apr 19, 2012 9:12 am

Re: CURL equivalent

Postby jeffrey.vandenhondel » Thu Jul 27, 2017 1:42 pm

And this on

Code: Select all
curl -H "Content-Type: multipart/form-data" -F "file=@factuur.pdf" -H
"Authorization: Bearer 88ds08fsd8fsdf" -X POST
http://example.com


Autorization part i know but its the form-data part i dont understand with the plugin.http.
jeffrey.vandenhondel
 
Posts: 36
Joined: Thu Jul 28, 2016 9:05 am

Re: CURL equivalent

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

jeffrey.vandenhondel wrote:And this on

Code: Select all
curl -H "Content-Type: multipart/form-data" -F "file=@factuur.pdf" -H
"Authorization: Bearer 88ds08fsd8fsdf" -X POST
http://example.com


Autorization part i know but its the form-data part i dont understand with the plugin.http.


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: 36
Joined: Thu Jul 28, 2016 9:05 am


Return to Plugins and Beans

Who is online

Users browsing this forum: No registered users and 5 guests