Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added more documentation on accessing results of completed request

This task calls a remote server to make a service call or retrieve an URL.

...

Code Block
languagejs
{
  "completed.name": "lang_da",
  "request.headers": [{
    "name": "Accept",
    "value": "application/json"
  }],
  "method": "GET",
  "body": null,
  "destination": "restcountries.eu",
  "description": "Get information for countries speaking danish",
  "class": "io.ceptor.pipeline.tasks.RetrieveURLTask",
  "uri": "https://restcountries.eu/rest/v2/lang/da"
}

Retrieving the results

Once the task is completed, the result is stored in the context (see Plugins and Scripts and Macros) in the completedRequests map, using the  value of the configured completed.name attribute as key

Code Block
public HashMap<String, CompletedRequest> completedRequests = new HashMap<>();


// CompletedRequest contains the result of a service call task within a pipeline
public final class CompletedRequest {
    public volatile long startedAt = System.currentTimeMillis();
    public volatile long endedAt;
    public volatile int tries;
    public volatile boolean isCancelled;
    public volatile Exception exception;
    public volatile byte[] response;
    public volatile int responseCode;
    public volatile String responseReason;
    public volatile HeaderMap responseHeaders;
 
    /* Returns the response converted to a string in the character set indicated by the content-type response header */
    public String getResponseAsString();
}


To access the results, you can either go into the structure directly from a script, e.g.

Code Block
var response = context.completedRequests.get("lang_da").getResponseAsString();
var httpResponseCode = context.completedRequests.get("lang_da").responseCode;

or you can use the macros to refer to the result, e.g. %{compreq.response:lang_da} or from a script:


Code Block
context.macro('%{compreq.response:lang_da}')

or from a script, call:

These macros are the relevant ones:

A macro is in the format %{<name>} or %{<prefix>:<name>}
The following prefixes related to accessing the result of a completed request exist:

  • compreq.cancelled - true, if the completed request was cancelled, e.g. because of timeout, and false if not.
  • compreq.exception - If the completed request failed with an exception, this contains the toString() output from the exception
  • compreq.response - The response for the completed request, converted to a string
  • compreq.response.code - Response code for a completed request, e.g. 200
  • compreq.response.reason - Response reason for a completed request, e.g. OK
  • compreq.response.headers - A JSON object containing all the response headers
  • compreq.response.header - The header value (or JSON array of values) of the response header - the name of the header stake from the # in the name, e.g. %{compreq.response.header:sample#Content-Length} returns the Content-Length header for the completed response named "sample".