CURL equivalent

Hi all.

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

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

or

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

Thanks in advance.

Best regards. Roberto Blasco

I am going to answer to myself :D

/** @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 !!!

Hi all.

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

curl --insecure --progress-bar -X POST -F 'number=9999999999' -F 'text=texto prueba' https://IP:44443/EnvioSMS.php

Best regards. Fernando Huertas.

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

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();

And this on

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:
And this on

curl -H "Content-Type: multipart/form-data" -F "file=@factuur.pdf" -H

“Authorization: Bearer 88ds08fsd8fsdf” -X POST




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.

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