IBM Streams 4.3.0
Processing REST API requests
To access information from IBM® Streams by using the REST API, your application must send a valid HTTP request. The application must then handle the HTTP response.
Before you begin
About this task
The objects that you can access with the IBM
Streams REST
API are referred to as resources.
Note: The
concept of REST API resources is different from the general concept
of resources in IBM
Streams.
A REST API resource refers an object with properties and relationships
to other resources. An IBM
Streams resource
refers to an entity on which stream processing applications and IBM
Streams services
can be run, for example a host. For more
information, see Resources.
Each resource can be identified by a uniform resource identifier (URI). You can also add query parameters to the URI to tailor and filter response content. The REST API supports standard HTTP methods for accessing the resource that is identified by the URI.
The IBM Streams REST API uses the JavaScript Object Notation (JSON) format for message content.
Procedure
Example
The following example Java™ code retrieves the root resource
information in a scenario where client authentication is enabled. In this example, user
authentication is handled by using HTTP basic
authentication:
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
import javax.xml.bind.DatatypeConverter;
import com.ibm.json.java.JSONObject;
public class RestBasic {
public static void main (String[] args) {
try {
System.setProperty("javax.net.ssl.trustStore", ".\\ibmjsse2.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "ibmpassw0rd");
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true; // trust all hosts that supply the Streams certificate
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
// Retain cookies between requests
CookieManager cm = new CookieManager();
CookieHandler.setDefault(cm);
URL url = new URL("https://server5.ibm.com:8456/streams/rest/domains/test6");
String userInfo = "bsmith" + ":" + new String("secret");
String authToken = "Basic " + DatatypeConverter.printBase64Binary(userInfo.getBytes());
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", authToken);
conn.setRequestMethod("GET");
conn.connect();
System.out.println("Response code: " + conn.getResponseCode());
System.out.println("Content type: " + conn.getHeaderField("Content-Type"));
InputStream responseStream = conn.getInputStream();
InputStreamReader responseStreamReader = new InputStreamReader(responseStream);
JSONObject jsonResult = JSONObject.parse(responseStreamReader);
String id = (String)jsonResult.get("id");
System.out.println("Domain id = " + id);
conn.disconnect();
}
catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}