Rest-post connector - upload file

Hello, 

in my process i have instantiation form contains a multiple file upload fields, 

in the contract i have multiple input of type FILE

the process has business variable named "files" of type java.util.List with default value the input contract previous

in the next service task (parallel-multi-instantiation) in the connector-in, i have connector of type REST-POST that call an endpoint that accept a formData parameter type to upload file.

I don't know the right way to pass the single file uploaded in the payload of the request e/o set the correct header of the connector.

The following image is the postman console request to the endpoint

Anyone can help me?

Thanks in advance

You can't open the image you attached, but I guess the content-type you want to use in the payload is "multipart/form-data", and that content-type I think Bonita's rest-post-connector doesn't support it yet. Instead you should create a Groovy script similar to the following or you can also use any library of your choice that allows HTTP Post requests:

def post = new URL("https://foo.bar/post").openConnection()
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "multipart/form-data")
post.getOutputStream().write(message.getBytes("UTF-8"))
post.............

SOLVED wink

 

Thank you Alex for your hint

I post the following code to help Bonita people to get file from contract and POST it to a REST API endpoint with authentication basic.

I hope this is useful as connector script in your processes ;)

 

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger
import org.bonitasoft.engine.bpm.contract.FileInputValue
import org.bonitasoft.engine.bpm.document.Document
import org.bonitasoft.engine.bpm.document.DocumentValue
import org.bonitasoft.engine.io.IOUtil
import java.nio.file.Files;

Logger logger = Logger.getLogger("org.bonitasoft");

String requestURL = "YOUR-URL";
String user = "YOUR-USERNAME";
String pass = "YOUR-PASSWD";
String userpass =  user + ":" + pass;
String authtoken = userpass.bytes.encodeBase64().toString();

FileInputValue fileInput = filesDaFirmare; // INSERT REFERENCE FROM FILEINPUTVALUE FROM CONTRACT


HttpURLConnection httpConn;
DataOutputStream request;
String boundary =  "*****";
String crlf = "\r\n";
String twoHyphens = "--";



// creates a unique boundary based on time stamp
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Cache-Control", "no-cache");
httpConn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
httpConn.setRequestProperty("Authorization", "Basic " + authtoken)
request =  new DataOutputStream(httpConn.getOutputStream());


// add File part
//
//
try {
    // get file from form input files list
    String fileName = fileInput.getFileName();
    String fieldName = "file";
    
    // write body
    request.writeBytes(twoHyphens + boundary + crlf);
    request.writeBytes("Content-Disposition: form-data; name=\"" +
            fieldName + "\";filename=\"" +
            fileName + "\"" + crlf);
    request.writeBytes(crlf);
    request.write(fileInput.getContent());
} catch(IOException e) {
    logger.severe("exception add file part " + e.toString())
}


// Completes the request and receives response from the server.
//
//
try {
    String response = "";
    request.writeBytes(crlf);
    request.writeBytes(twoHyphens + boundary +
            twoHyphens + crlf);
    
    request.flush();
    request.close();
    
    // checks server's status code first
    int status = httpConn.getResponseCode();
    if (status == HttpURLConnection.HTTP_OK) {
        InputStream responseStream = new BufferedInputStream(httpConn.getInputStream());
    
        BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
    
        String line = "";
        StringBuilder stringBuilder = new StringBuilder();
    
        while ((line = responseStreamReader.readLine()) != null) {
            stringBuilder.append(line).append("\n");
        }
        responseStreamReader.close();
    
        response = stringBuilder.toString();
        
        httpConn.disconnect();
    } else {
        logger.severe("Server returned non-OK status: " + status)
    }
    
    return response;
} catch(IOException e) {
    logger.severe("exception Completes the request " + e.toString())
}