Implementing REST API operation processing in the subflow by using message flow nodes

Access the REST API operation information, REST API operation parameters, and REST API request and response body.

Accessing the REST API operation information

Information about the current operation is automatically placed into the local environment tree. You can use this information in your implementation if you want to determine which operation in the REST API was called, which HTTP method was used, the request path, or the request URI. For example, if you are sharing transformation logic across multiple operations, you can use this information to determine in which operation the REST API was called.

Accessing current operation information in a message map
You can access the current operation information in a message map. The current operation information is available as a set of elements, which are named Method, Operation, Path, and URI, in a Message Assembly that includes the Local Environment, under Local Environment > REST > Input folder. For more information, see Mapping data in the local environment tree.

When you create the message map by using the option Message map with input and output for REST API operation, the local environment is automatically added to the map input and any path or query parameters are added. A Task transform is pre-wired to Local Environment > REST > Input, to help you locate this information. For more information, see Implementing a REST API operation by using a message map.

Accessing current operation information in ESQL

Check to see if the current operation was called by using an HTTP GET.

IF InputLocalEnvironment.REST.Input.Method = 'GET' THEN
  -- Executed only if the current operation was called using an HTTP GET.
END IF;

Check to see if the current operation is the getAllCustomers operation.

IF InputLocalEnvironment.REST.Input.Operation = 'getAllCustomers' THEN
  -- Executed only if the current operation is the getAllCustomers operation. 
END IF;

Create a log message with the request path.

DECLARE logMessagePath CHARACTER 'Received request on path ' || InputLocalEnvironment.REST.Input.Path;

Create a log message with the request URI.

DECLARE logMessageURI CHARACTER 'Received request on URI  ' || InputLocalEnvironment.REST.Input.URI;
Accessing current operation information in Java™

Check to see if the current operation was called by using an HTTP GET.

MbMessage inLocalEnvironment = inAssembly.getLocalEnvironment();
MbElement leRestInput = inLocalEnvironment.getRootElement().getFirstElementByPath("/REST/Input");

if (leRestInput.getFirstElementByPath("Method").getValueAsString().equals("GET")) {
  // Executed only if the current operation was called using an HTTP GET.
}

Check to see if the current operation is the getAllCustomers operation.

if (leRestInput.getFirstElementByPath("Operation").getValueAsString().equals("getAllCustomers")) {
  // Executed only if the current operation is the getAllCustomers operation. 
}

Create a log message with the request path.

String logMessagePath = "Received request on path " + leRestInput.getFirstElementByPath("Path").getValueAsString();

Create a log message with the request URI.

String logMessageURI = "Received request on URI  " + leRestInput.getFirstElementByPath("URI").getValueAsString();
Accessing current operation information in .NET (C#)

Check to see if the current operation was called by using an HTTP GET.

NBMessage inLocalEnvironment = inputAssembly.LocalEnvironment;
NBElement leRestInput = inLocalEnvironment.RootElement["REST"]["Input"];

if (((String) leRestInput["Method"]) == "GET"){
  // Executed only if the current operation was called using an HTTP GET.
}

Check to see if the current operation is the getAllCustomers operation.

if (((String) leRestInput["Operation"]) == "getAllCustomers") {
  //Executed only if the current operation is the getAllCustomers operation. 
}

Create a log message with the request path.

String logMessagePath = "Received request on path " + ((String) leRestInput["Path"]);

Create a log message with the request URI.

String logMessageURI = "Received request on URI  " + ((String)leRestInput["URI"]);

Accessing the REST API operation parameters

If the definition of an operation includes one or more parameters, the names and values of those parameters are automatically placed into the local environment tree, but only if those parameters are provided by the HTTP client that is calling the operation. Optional or missing parameters are not placed into the local environment tree. Parameters are always placed into the local environment tree as character (string) elements, where the name of each element is the name of the parameter and the value of the element is the value of the parameter.

Accessing parameter values in a message map
You can access parameter values in a message map. All parameters are available as set of elements, where the elements have the same name as the parameter name, in a Message Assembly that includes the Local Environment, under Local Environment > REST > Input > Parameters folder.

When you create the message map by using the option Message map with input and output for REST API operation, the local environment is automatically added to the map input and any path or query parameters are added under Local Environment > REST > Input > Parameters, ready for you to connect transforms. A Task transform is pre-wired to Local Environment > REST > Input , to help you locate this information. For more information, see Implementing a REST API operation by using a message map. If you update the REST API and change the path or query parameters, the message map is updated with the new definitions when it is reopened. If there were existing transforms wired to the parameters, they might need to be modified.

If you do not use the option Message map with input and output for REST API operation, you must add the local environment manually, and in the Parameters folder use the any element to add user-defined elements. For more information, see Mapping data in the local environment tree and Defining user-defined elements.

Accessing parameter values in ESQL

Check to see if the parameter named 'max' is supplied.

DECLARE max INTEGER -1;
IF FIELDTYPE(InputLocalEnvironment.REST.Input.Parameters.max) IS NOT NULL THEN
  SET max = InputLocalEnvironment.REST.Input.Parameters.max;
END IF;
Accessing parameter values in Java

Check to see if the parameter named 'max' is supplied.

MbElement maxElement = inLocalEnvironment.getRootElement().getFirstElementByPath("/REST/Input/Parameters/max");
int max = -1;
if (maxElement != null) {
  max = Integer.valueOf(maxElement.getValueAsString());
}
Accessing parameter values in .NET (C#)

Check to see if the parameter named 'max' is supplied.

NBElement maxElement = inLocalEnvironment.RootElement["REST"]["Input"]["Parameters"]["max"];
int max = -1;
if (max != null){
  max = (int) maxElement;
}

Accessing the REST API request and response body

Depending on the HTTP method of the operation, the operation can accept data from the HTTP client in the request body. REST APIs in IBM® Integration Bus are configured by default to process JSON data. For more information about processing JSON data that was passed in the request body, and creating JSON data to send as the response body, see JSON parser and domain. You can also use a message map to process JSON data. For more information, see Creating or transforming a JSON output message by using a message map.