Page 1 of 1

What Type of value REST API is expecting for Input document on case?

Posted: Fri Jul 14, 2017 6:20 am
by sanjaygupta15
What Type of value REST API is expecting for Input document on case? Byte Array or base64 string or file path only which can be accessible from the REST API hosting server.
Can I send the File Upload request in JSON format? Please provide detail and which format i need to pass the document details.

Re: What Type of value REST API is expecting for Input document on case?

Posted: Fri Jul 14, 2017 7:47 pm
by amosbatto
There is currently no way to send an Input Document in JSON to ProcessMaker with REST. For that reason, you have to use PHP, Python, Java or a similar server side language to send files to ProcessMaker with REST. It is currently not possible with JavaScript (or a similar browser-side language).

See: http://wiki.processmaker.com/3.0/REST_A ... t-document

What programming language are you using?

I don't recommend doing this, but there are workarounds. For example, you could create a trigger like this in one of your ProcessMaker processes:
Code: Select all
if (isset(@@sFileBase64) and !empty(@@sFileBase64)) {
    $sContentsDecoded = base64_decode(@@sFileBase64);
    $sFilename = tempnam("C:\mydir", "pm_");
    if ($sFilename === false) {
      throw new Exception("Unable to create temporary file");
    } 
    file_put_contents($sFilename, $sContentsDecoded);
    $inpDocId = "XXXXXXXXXXXXXXXXXXXXXXXXX";  //set to the unique ID of the Input Document
    
    @@newFileId = PMFAddInputDocument($inpDocId, null, 1, 'INPUT', '', 'Add',
       @@APPLICATION, @%INDEX, @@TASK, @@USER_LOGGED, 'file', $sFilename);

     if (!empty(@@newFileId)) {
                require_once 'classes/model/AppDocument.php';
		$aProps = array(
		   'APP_DOC_UID'     => @@newFileId,
		   'DOC_VERSION'     => 1,
		   'APP_DOC_FILENAME'=> @@newFilename
		);
		
		$oDoc = new AppDocument();
		$oDoc->update($aProps);
    }
}
Note: If using version 3.2, you will need to fix the source code for PMFAddInputDocument(). See: http://bugs.processmaker.com/view.php?id=22881

Then, in your external script, you need to convert the file to base64 and store it in a variable. For example, in PHP, you could do something like this:
Code: Select all
$sFileBase64 = base64_encode(file_get_contents("C:\MyDir\MyFile.ext"));
$newFilename = "MyFile.ext";
Then, send sFileBase64 and newFilename as variables to a case in ProcessMaker with the
PUT /cases/{app_uid}/variable endpoint.
Then, execute the above trigger with the PUT /cases/{app_uid}/execute-trigger/{tri_uid} endpoint.

I haven't tried the above code, so it might take some debugging, but it gives you the basic idea. If you are using JavaScript, see: https://www.nczonline.net/blog/2012/05/ ... pt-part-1/

Re: What Type of value REST API is expecting for Input document on case?

Posted: Mon Jul 17, 2017 1:18 am
by sanjaygupta15
Thanks for response amosbatto!.

I am using C# language for consuming the REST API to upload the document. How i can sent the document to the REST API? If you have any sample then please share.

Re: What Type of value REST API is expecting for Input document on case?

Posted: Tue Jul 18, 2017 9:49 pm
by amosbatto
Sorry, I don't know C#, so I can't help you much, however, this looks like the way to do it:
https://stackoverflow.com/questions/113 ... th-c-sharp