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

Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.

Moderator: amosbatto

Forum rules
Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
sanjaygupta15
Posts: 2
Joined: Fri Jul 14, 2017 6:10 am
Country: India

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

Unread postby sanjaygupta15 » Fri Jul 14, 2017 6:20 am

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.

amosbatto
Posts: 5245
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz

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

Unread postby amosbatto » Fri Jul 14, 2017 7:47 pm

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_API_Cases#Upload_Input_Document:_POST_.2Fcases.2F.7Bapp_uid.7D.2Finput-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/08/working-with-files-in-javascript-part-1/
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA-A

sanjaygupta15
Posts: 2
Joined: Fri Jul 14, 2017 6:10 am
Country: India

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

Unread postby sanjaygupta15 » Mon Jul 17, 2017 1:18 am

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.

amosbatto
Posts: 5245
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz

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

Unread postby amosbatto » Tue Jul 18, 2017 9:49 pm

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/1131425/send-a-file-via-http-post-with-c-sharp
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA-A


Return to “Developing processes & programming”

Who is online

Users browsing this forum: No registered users and 2 guests