Creating entities

You can create and insert entities by sending a REST API POST request to the server.

About this task

Using a POST request, you can instruct the server to store an entity in the solution location that is indicated by the URI. If the entity does not exist, the server creates the resource by using entity content that is supplied in the POST request. If the entity exists, the request fails and the status code 409 Conflict is displayed in the returned response. The POST request does not overwrite or update an existing entity. Send a POST request to the server by using a script, such as Jython, or from a REST client tool that works with your browser.

Procedure

  1. Make sure you can access the Insight Server by entering the generic REST URL in the browser address field. See Managing entities for an example of the REST URL.
  2. Create the POST request by using the syntax that is shown in the example.

    Depending on the value of the httpEndpoint setting in the server.xml file on your system, the first part of the request URL is http://hostname:9080 for httpPort connections, or https://hostname:9443 for httpsPort connections.

    .../solutions/solution_name/entity-types/entity_type/entities
    where solution_name is the symbolic solution name, for example BankingSolution, and entity_type is a category of entity such as person or customer.

Results

The response to a successful POST request includes the status code 201 Created and the location header that provides the path to the new entity. If the entity insertion failed, the status code 409 Conflict is returned to indicate that the entity exists.

Example

The following example shows a POST request that is embedded in a script.
public void doPOSTUsingRESTRequest() throws Exception {
TestDriver driver = new TestDriver();
ConceptFactory cf = driver.getConceptFactory(ConceptFactory.class);

// Create a recipient entity in the hello_world solution
Recipient recp1 = cf.createRecipient("REP003");
recp1.setHelloCount(10000);

String solutionName = "hello_world";
String entityType = recp1.get$TypeName();

// com.ibm.ia.test.helloworld.Recipient
DriverProperties properties = new DriverProperties();
String runtimeHostName = properties.getproperty(
DriverProperties.RUNTIME_HOST_NAME, "localhost");

String url = "https://" + runtimeHostName
+ ":9443/ibm/ia/rest/solutions/" + solutionName
+ "/entity-types/" + entityType;
System.out.println(url);

HttpsURLConnection connection = getConnection(url, "POST", "application/xml");
driver.getModelSerializer().serializeEntity(DataFormat.GENERIC_XML,
connection.getOutputStream(), recp1, null);
connection.connect();
String content = getContent(connection, HttpURLConnection.HTTP_OK);
System.out.println(content);
connection.disconnect();
}