Using predefined generic event definitions to invoke HTTP services with JSON and string payloads
JSON payloads
You can invoke an HTTP service with a JSON payload by using predefined generic Apama event definitions. To do so, you have to use the
JSON with generic request/response event definitions option when adding the HTTP client connectivity plug-in. See also
Adding the HTTP client connectivity plug-in to a project.
This
generic option uses a predefined chain definition with dynamic chain instances to invoke multiple HTTP services, and it uses event types in the
com.softwareag.connectivity.httpclient package. For detailed information about the available event types, see the
API Reference for EPL (ApamaDoc) .
The following example shows how to invoke an HTTP service using the generic events:
action performRequest() {
// 1) Get the transport instance
HttpTransport transport := HttpTransport.getOrCreate("www.example.com", 80);
// 2) Create the request event
Request req:= transport.createGETRequest("/geo/");
// 3) Execute the request and pass the callback action
req.execute(handleResponse);
}
action handleResponse(Response res) {
// 4) Handle the response
if res.isSuccess() {
// 5) Extract data from the payload
log res.payload.getString("location.city") at INFO;
} else {
log "Failed: " + res.statusMessage at ERROR;
}
}
Overriding the content-type header of an HTTP request to allow non-JSON string payloads
You can override the content-type header of an HTTP request to allow for non-JSON string payloads.
Whenever the content-type header of a request is not overridden, the payloads are encoded as JSON (this is the default setting). When you override the content-type header, the JSON codec is skipped and the payload is not encoded as JSON, allowing string data to be passed through. The decoding of the response to the request depends on the content type provided by the server.
The following example demonstrates how to override an HTTP request's content-type header to send string data:
// 1) HTTP PUT request with string ("example string payload") payload
req := transport.createPUTRequest("/plain_string", "example string payload");
// 2) Override the request's content-type header
req.setHeader("content-type", "text/plain");
// 3) Execute the request, passing the callback action handleResponse
req.execute(handleResponse);
Enabling and controlling concurrency
You can create an instance of a transport which uses multiple clients by providing the number of clients when creating it:
HttpTransport transport :=
HttpTransport.getOrCreateWithConfigurations("www.example.com", 80,
{ HttpTransport.CONFIG_NUM_CLIENTS: "3" });
When creating a request, you can specify the concurrency control key or flush behavior for requests that have some dependencies:
Request req:= transport.createGETRequest("/geo/");
req.setConcurrencyControlKey("geo");
req.setConcurrencyControlFlush(true);
req.execute(handleResponse);
For more information on concurrency in the HTTP client, see
Executing HTTP requests concurrently.