Passing files

How to use JavaScript to invoke REST services that have an input parameter or a response of type file or of type string and format binary.

See the following sections:
Input parameters:
Type file
Invoking a REST service with an input parameter of type file
Type string and format binary
Invoking a REST service with an input parameter of type string and format binary
Output parameters:
Type file
Invoking a REST service that returns a file as response
Type string and format binary
Invoking a REST service that returns a response of type string and format binary
Restriction: Content type application/x-www-form-urlencoded is not supported.

Invoking a REST service with an input parameter of type file

In Business Automation Workflow, if the OpenAPI specification (formerly known as Swagger) for a REST service specifies an input parameter of type file, it is mapped to the Business Automation Workflow document type IBM_BPM_Document. This means that your process or service flow uses a reference to a document in the BPM document store and the ECMDocumentInfo input object refers to that file. For more information about using the embedded BPM document store, see Working with BPM documents.

The following example illustrates how to provide a reference to a document in the BPM document store.

// Prepare REST request
var request = new BPMRESTRequest();

// Specify the external service and its operation to be invoked
request.externalServiceName = "MyExternalService";
request.operationName = "addContract";

// 
// Set the http headers
var headers = {"Content-Type": "multipart/form-data"};
request.httpHeaders = headers;

// Provide a reference to the document that is to be used as input parameter of the REST service
var contractFile = new tw.object.ECMDocumentInfo();
contractFile.objectId = "idd_808F0A58-0000-C214-BE0C-E24DEF2EA51E"; 

// Prepare all parameters that are needed for the REST request
request.parameters = {"file": contractFile};

// Execute the REST request, which returns a BPMRESTResponse instance.
var response = tw.system.invokeREST(request);

// have a look at the response
For more information about the ECMDocumentInfo, BPMRESTRequest and BPMRESTResponse objects, see the reference topic in JavaScript API.

Invoking a REST service with an input parameter of type string and format binary

In Business Automation Workflow, if the OpenAPI specification (formerly known as Swagger) for a REST service specifies an input parameter of type string and format binary, it is mapped to the Business Automation Workflow document type IBM_BPM_Document. This means that your process or service flow uses a reference to a document in the BPM document store and the ECMDocumentInfo input object refers to that file. For more information about using the embedded BPM document store, see Working with BPM documents.

The following example illustrates how to provide a reference to a document in the BPM document store.

// Prepare REST request
var request = new BPMRESTRequest();

// Specify the external service and its operation to be invoked
request.externalServiceName = "MyExternalService";
request.operationName = "addContract";

// Provide a reference to the document that is to be used as input parameter of the REST service
var contractFile = new tw.object.ECMDocumentInfo();
contractFile.objectId = "idd_808F0A58-0000-C214-BE0C-E24DEF2EA51E"; 

// Prepare all parameters that are needed for the REST request
request.parameters = {"file": contractFile};

// Execute the REST request, which returns a BPMRESTResponse instance.
var response = tw.system.invokeREST(request);

// have a look at the response
For more information about the ECMDocumentInfo, BPMRESTRequest and BPMRESTResponse objects, see the reference topic in JavaScript API.

Invoking a REST service that returns a file as response

In Business Automation Workflow, if the OpenAPI specification for a REST service specifies a response of type file, the file is stored as a document of type IBM_BPM_Document in the BPM document store. A reference to the document is returned to your service flow in an object of business object type ECMDocumentInfo.

The following example illustrates how to receive a file as response from a REST service invocation.

// Prepare REST request
var request = new BPMRESTRequest();

// Specify the external service and its operation to be invoked
request.externalServiceName = "MyExternalService";
request.operationName = "getContract";

// Set the accept header
var headers = {"Accept": "application/pdf"};
request.httpHeaders = headers;

// Prepare the parameters that are needed for the REST request
request.parameters = {"contractId": "1234567890"};

// Execute REST request which returns a BPMRESTResponse instance.
var response = tw.system.invokeREST(request);

// Have a look at the response
log.info("Response - httpStatusCode: " + response.httpStatusCode);
log.info("Response - httpStatusMessage: " + response.httpStatusMessage);
log.info("Response - httpHeaders: " + response.httpHeaders);
log.info("Response - content: " + response.content);

// The response file is stored as a document in the BPM document store. 
// An instance of ECMDocumentInfo is returned that contains the details.
var documentInfo = JSON.parse(response.content);
log.info("Response - documentInfo.objectId: " + documentInfo.objectId);
For more information about the ECMDocumentInfo, BPMRESTRequest and BPMRESTResponse objects, see the reference topic in JavaScript API.

For more information about using the BPM document store, see Working with BPM documents.

Invoking a REST service that returns a response of type string and format binary

In Business Automation Workflow, if the OpenAPI specification for a REST service specifies a response of type string and format binary, the response is stored as a document of type IBM_BPM_Document in the BPM document store. A reference to the document is returned to your service flow in an object of business object type ECMDocumentInfo.

For more information about the ECMDocumentInfo, BPMRESTRequest and BPMRESTResponse objects, see the reference topic in JavaScript API.

For more information about using the BPM document store, see Working with BPM documents.