The JavaScript REST Client communication point enables users to make REST calls against configured REST clients by executing a user-configured script to handle the connection and response. The connection itself is established by the Rhapsody REST client framework, and the custom JavaScript code performs the HTTP calls.
Supported Operational Modes: Output, Out->In.
Configuration Properties
Output Mode
Property |
Description |
---|---|
REST Client |
The user-selected REST client that is bound to the JavaScript code. |
Request JavaScript |
The JavaScript code that is executed when the communication point is run. Refer to JavaScript REST Objects for details on the objects associated with the scripts used by the JavaScript REST Client communication point. |
Read Timeout (ms) |
How long to wait (in milliseconds) when trying to read a response from the server. This property applies to each request made in the script. A value of zero or less is treated as an infinite timeout. |
Out->In Mode
The configuration properties for the JavaScript Rest Client communication point in Out->In mode is the same as Output mode. Refer to Out->In and In->Out Properties for general details on the Out->In mode for communication points.
When in this mode, an additional output
object is available for use within JavaScript. Through the output
object you can set the message body and properties based on the response received.
JavaScript REST Objects
You can utilize the following REST-specific JavaScript objects in the Request JavaScript property to configure the JavaScript REST Client communication point:
Object | Description |
---|---|
client |
A REST client object used for invoking REST requests. The requests are invoked synchronously and a response object returned. |
logger |
A log object that enables logging to the Rhapsody log files.
Within the JavaScript REST Client communication point, the log object is named
logger , not
log .
|
The output
object (used to define the message received on a route) is only available if the communication point is in Out->In mode. The standard input
object is available in Output and Out->In modes. Refer to:
- the JavaScript filter for details on the use of JavaScript within Rhapsody.
- JavaScript Object Reference for details on general JavaScript objects that Rhapsody exposes.
If you are processing JSON messages, use the JSON object.
Client Object
The client
object is used from within the script to invoke the REST request using the methods GET, POST, DELETE, and PUT. These methods take the request configuration as a parameter, perform the REST request synchronously and return a response object. The configured REST client is used to perform these operations.
Method | Format | Description |
---|---|---|
get |
response = client.get(configuration) |
Performs an HTTP GET request and returns the response. |
post |
response = client.post(configuration, data, type) |
Performs an HTTP POST request and returns the response. You can set an optional parameter |
destroy |
response = client.destroy(configuration) |
Performs an HTTP DELETE request and returns the response. |
put |
response = client.put(configuration, data, type) |
Performs an HTTP PUT request and returns the response. You can set an optional parameter |
options |
response = client.options(configuration) |
Performs an HTTP OPTIONS request and returns the response. |
head |
response = client.head(configuration) |
Performs an HTTP HEAD request and returns the response. |
Configuring the Request Options
When calling the GET, POST, DELETE or PUT methods on the client
object, a request configuration is passed as a parameter. This request configuration should be created in the script defined on the JavaScript REST Client communication point.
The following options can be defined in the request configuration:
Request Option | Description |
---|---|
|
Defines the Accept request-header field. This can be used to specify certain media types which are acceptable for the response. Example: |
relative-url |
Defines the URL path to use relative to the URL defined within the REST client configured on the communication point. Example: |
headers |
Defines the request headers to be included in the request sent. These should be defined in an array as shown in the example here. Multiple header values can be defined by delimiting the values with commas. Example: |
parameters |
Defines the request parameters to be included in the request sent. These should be defined in an array. Example: |
content-type |
Defines the Content-Type request header field. This is used to declare the MIME type of the request body, and is mostly useful for PUT and POST requests. Example: |
enable-response-handling |
Defines whether the configured response actions on the REST client should be ignored. By default, this option is set to true, which enables default response handling. Example: |
remove-headers |
Defines headers from the REST client configuration to remove. These are removed before extra headers are added via the headers option. Example: |
var config = { 'accept-type': 'application/xml+fhir', 'relative-url': '/patient/1', 'headers': [{'header':'headerA','value':'a'}, {'header':'headerB','value':'b'}], 'parameters': [{'parameter':'a','value':'a'}], 'content-type': 'application/xml', 'remove-headers': ['header1', 'header2'] }; var response = client.get(config);
var config = { 'relative-url': '/patient/2', 'headers': [{'header':'headerA','value':'a'}, {'header':'headerB','value':'b'}], 'parameters': [{'parameter':'a','value':'a'}], 'content-type': 'application/xml' }; var messageBody = input.text; var response = client.post(config, messageBody);
Default Response Handling
The default response handling options are configured on the REST client. Using the REST client response actions, the JavaScript REST Client communication point can be configured to throw a connection or message error based on the status code response to a REST request. For example, when the selected REST client has On 4xx action configured as Treat as message error
, and has a status code response of 404 is received the communication point will stop executing the configured JavaScript at the point the request was made, and move the message to the Error Queue. If instead the response action On 4xx action is configured as Treat as connection error
, then a connection error is raised and the communication point attempts to reprocess the message through the configured JavaScript up to the number of allowed connection retries configured on the communication point.
The default response action handling can be disabled from the JavaScript REST Client communication point by setting the enable-response-handling
request option to false.
If you configure the JavaScript on the communication point to make multiple REST requests within a single JavaScript REST client, the requests are not atomic. For example, if a connection error is raised when the second REST request is performed, the first REST request is not rolled back and will be re-invoked as the communication point re-attempts to process the message at the beginning of the configured JavaScript on the next connection attempt.
Custom Response Handling
The JavaScript REST Client provides custom response handling in the request script for sending errored messages to the Error Queue or retrying connection by treating responses as a connection error.
The enable-response-handling
option in the request configuration must be set to false for custom response handling to work correctly.
SendingMessageError Object
Object |
Description |
---|---|
|
Create a sending message error with the cause response to send the message to the Error Queue.
|
ConnectionError Object
Object |
Description |
---|---|
|
Create a connection error with the cause response to retry the connection.
|
var config = { 'accept-type': 'application/xml+fhir', 'relative-url': '/patient/1', 'headers': [{'header':'headerA','value':'a'}, {'header':'headerB','value':'b'}], 'parameters': [{'parameter':'a','value':'a'}], 'content-type': 'application/xml', 'remove-headers': ['header1', 'header2'], 'enable-response-handling' : 'false' }; var response = client.get(config); if (response.status == 200) { // When response was valid but has empty body, send message to Error Queue. if (response.response == "") { throw new SendingMessageError(response, 'Patient data returned empty'); } } else if (response.status >= 400) { throw new ConnectionError(response); }
Checking the Response Object
The response object returned from a GET, POST, PUT or DELETE method can be inspected to check the status code returned, the body of the response, and the response headers returned.
Method | Description |
---|---|
status |
Returns the HTTP status code returned by the server to the request. For example, Example: response.status |
response |
Returns the body of the HTTP response as a string. Example: |
responseBytes |
Returns the body of the HTTP response as a byte array. Example: |
headers |
Returns an array of the HTTP response headers. Examples:
|
If connection fails, for example when the hostname to connect to cannot be resolved or an incorrect port number is used, then no response object is returned. Instead, an exception is thrown and the message placed on the Error Queue.
Output Data Encoding
As of Rhapsody 6.3, the encoding of the output data for PUT and POST methods is determined as follows:
- If the output data is a byte array (or the optional
type
parameter is set tobytes)
, then no encoding is performed and the output data is sent as is. - If the
charset
parameter is provided in thecontent-type
header, then the encoding specified in the parametercharset
is used to encode the output data.
For example:Content-Type: application/json; charset=UTF-16
. - If the
charset
parameter is not provided in thecontent-type
header, then the encoding of the output message is used to encode the output data.
In previous versions of Rhapsody, the encoding of the output data is set to the default encoding of the operating system the engine is running on (for example, the default Windows® encoding).