Accept a file from REST API
You can use the below functions to store that file in a document variable via connectors in Bonita. Please note that the documentName parameter in the below functions refer to the name you provide while defining the document variable in the process
Document attachDocument(long processInstanceId,
String documentName,
String fileName,
String mimeType,
String url)
throws ProcessInstanceNotFoundException,
DocumentAttachmentException
Ref: attach document by url
Document attachDocument(long processInstanceId,
String documentName,
String fileName,
String mimeType,
byte[] documentContent)
throws ProcessInstanceNotFoundException,
DocumentAttachmentException
Ref: attach document by byte array of the file
Comments
thanks Raji
your post helped me a lot, but the problem right now is how to access the file bytes from my API response in the connector.
I'm using bonita 7.6 and I have these variable in the connector output operations: bodyAsObject, bodyAsString and headers. I tried to cast the bodyAsObject to byte[] but it failed
Do you have any idea how to get the file bytes steam in the connector output operation?
Hello,
You can leverage "bodyAsString" attribute and convert the string to bytes. I gave a quick try and this worked for me.
apiAccessor.processAPI.attachDocument(processInstanceId,
<document var name>,
<document name>,
null,
bodyAsString.getBytes());
I tried that, the file(.xlsx) is attached to the task and able to download it from the task overview page, but the file is corrupted when tried to open it. I tried to request my API from the browser and the file is downloaded and opened successfully.
here is my code
do you have any idea why this happened?
I tried an audio file for my testing which worked.
both didn't work for me.
I tried to pass the URL instead of the bytes array, but when I tried to download the file I got this error
its concatenating the the url of bonita + the file url that I passed to attached document
here is my code
final ProcessAPI processAPI = apiAccessor.getProcessAPI()
processAPI.attachDocument(processInstanceId, "inputfile", "logo.png",null,
"https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")
I found your URL working too. Here is the code I used.
P.S.:
1. If you would want to know where attachDocument by URL can be used, you might want to read this section .
2. Referred this answer to get byte array from an URL.