Calling decision services with the REST API
A client that sends an HTTP request to the Decision Server Runtime must include an
Authorization
header that contains an encoded username and password.
About this task
When you send an HTTP or HTTPS request from a client, you must add an
Authorization
that contains the word Basic
, followed by a space
and a base64-encoded string of the username:password
. Basic authentication is a
simple authentication scheme that is built into the HTTP protocol, and is enabled by default in ODM
on Certified Kubernetes. In other words, the
Authorization
must contain the authentication information of the client for the
resource that is requested.
For example, to authorize the predefined user and password resAdmin:resAdmin
, a
client must include the following header in the request: Authorization: Basic
cmVzQWRtaW46cmVzQWRtaW4=
.
If you defined users and groups from an LDAP directory, for example, you enter a base64-encoded real user name and password from your company. For more information, see Configuring user access.
A client application calls a decision service by creating a secure connection with a server through the REST API. The endpoint URI for a decision service uses the following format:
http://<host>:<port>/DecisionService/rest/v1/<ruleset_path>
The execution of a ruleset is done through an HTTP POST command that specifies an endpoint URI. For more information about the REST API, see Executing rules by using the REST service.
Example
The following example shows a Java™ client that calls a ruleset in the MiniloanService
service with basic authentication.
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class DecisionServiceExecution {
public static void main(String[] args) throws IOException {
// Replace <host> and <port>
String endpointURI = "http://<host>:<port>/DecisionService/rest/v1/MiniloanService/MiniloanServiceRuleset";
String contentType = "application/json";
String username = "resExecutor";
String password = "resExecutor";
String auth = new StringBuffer(username).append(":").append(password).toString();
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes());
String authHeader = "Basic " + new String(encodedAuth);
// Set the borrower and the loan
String payload = "{\"borrower\": {" +
"\"name\": \"John\"," +
"\"creditScore\": 600," +
"\"yearlyIncome\": 80000" +
"}," +
"\"loan\": {" +
"\"amount\": 500000," +
"\"duration\": 240," +
"\"yearlyInterestRate\": 0.05" +
"}" +
"}";
CloseableHttpClient client = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(endpointURI);
httpPost.setHeader("Content-Type", contentType);
httpPost.setHeader("Authorization", authHeader);
httpPost.setEntity(new StringEntity(payload));
CloseableHttpResponse response = client.execute(httpPost);
try {
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
System.err.println("Status Code: " + response.getStatusLine().getStatusCode());
System.err.println("Status Line: " + response.getStatusLine());
String responseEntity = EntityUtils.toString(response.getEntity());
System.err.println("Response Entity: " + responseEntity);
throw new RuntimeException(
"An error occurred when invoking Decision Service at: "
+ endpointURI
+ "\n"
+ response.getStatusLine() + ": " + responseEntity);
} else {
String result = EntityUtils.toString(response.getEntity());
System.out.println("Result: " + result);
}
} finally {
if (response != null) {
response.close();
}
}
} finally {
client.close();
}
}
}