0 votes
1.6k views
in Integrations by

1 Answer

0 votes
by (30.6k points)

From a service (server side) you have three ways to call a REST API, in order of preference:

  1. REST external service (BPM 8.5.7+ / BAW): import the Swagger / OpenAPI, get typed business objects and a Server definition per environment - no code (question 1057).
  2. BPMRESTRequest + tw.system.invokeREST for BPM's own REST API (tasks, instances, users) - question 3127.
  3. Java's HTTP client from a script for any other API without a Swagger:
// service flow script: POST JSON, read JSON (works on every 8.5.x / BAW release; needs the endpoint's signer in the truststore for https)
var url = tw.env.crmBaseUrl + "/customers";
var con = new java.net.URL(url).openConnection();
con.setRequestMethod("POST"); con.setConnectTimeout(10000); con.setReadTimeout(30000); con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Authorization", "Bearer " + tw.local.token);            // or Basic (question 67)
var payload = JSON.stringify({ name: tw.local.customer.name, email: tw.local.customer.email });
var os = con.getOutputStream(); os.write(new java.lang.String(payload).getBytes("UTF-8")); os.close();
var status = con.getResponseCode();
var stream = status >= 400 ? con.getErrorStream() : con.getInputStream(), text = "", line;
var rd = stream == null ? null : new java.io.BufferedReader(new java.io.InputStreamReader(stream, "UTF-8"));
while (rd != null && (line = rd.readLine()) != null) text += line;
if (rd != null) rd.close();
if (status >= 300) throw new Error("CRM " + status + ": " + text.substring(0, 300));
var json = JSON.parse(text);
tw.local.customer.id = json.id;

Rules: base URLs, credentials and timeouts from environment variables / Server definitions; always set timeouts (a hanging call blocks a thread); throw on HTTP errors so that the BPD's error handling sees them; wrap the code in a toolkit service ("REST Call" with method, url, headers, body in - status, body out, question 2888) so that apps do not repeat it; on hardened servers where scripts may not use Java, use a Java integration with the same code. Client side (coach) calls are a different topic (question 756).

References

Related questions

0 votes
1 answer 2.9k views
0 votes
1 answer 823 views
0 votes
1 answer 1.0k views
0 votes
1 answer 675 views
0 votes
1 answer 1.6k views
0 votes
1 answer 1.9k views
0 votes
1 answer 2.1k views
0 votes
1 answer 764 views
0 votes
1 answer 2.4k views
0 votes
1 answer 2.3k views

723 questions

807 answers

98 comments

4.9k users

Join BPM Community Discord Channel

Welcome to BPM Tips Q&A, Community wiki/forum where you can ask questions and receive answers from other IBM BPM experts and members of the community. Users with 2000 points will automatically be promoted to expert level.
Created by Dosvak LLC
Our Youtube Channel
...