VCF Operations Orchestrator allows you to define endpoints for API integration in the inventory as RestHosts using the HTTP-REST plugin. Once a RestHost is defined, it is then possible to authenticate and perform HTTP web requests on the endpoint (GET, POST, etc).
HttpRestClient is designed to enhance this experience by doing all the heavy lifting when performing these requests, such as handling of different content types, error handling and retry logic.
Here are some of the benefits and features provided by the HttpRestClient:
- Provides support for the following HTTP methods: GET, POST, PUT, PATCH, DELETE, HEAD;
- Retries a failed connection (default to 5 attempts) with a delay specified in seconds (defaults to 10 seconds);
- Option to specify the expected response codes (defaults are set per method);
- Option to retry on 500 status code (default enabled);
- Automatic handling of “application/x-www-form-urlencoded” content;
- Option to set Accept-Type header (defaults to application/json);
- Option to set Content-Type header (defaults to Accept-Type);
- Automatic URI and URI Component encoding (detects if existing encoding is present);
- Obfuscates secrets in content from log output that match password/secret/refreshToken;
The HttpRestClient serves as the backbone for API integration between VCF Operations Orchestrator and the API endpoint and is designed to integrate with or extend any API service.

You can download my HttpRestClient module as a package here or as native JS here.
Using the HttpRestClient
Using the HttpRestClient in an Action or Workflow simply requires the following to import the module:
As a variable:
var rest = new (System.getModule("com.simplygeek.rest").HttpRestClient())(restHost);
As an object property:
this.rest = new (System.getModule("com.simplygeek.rest").HttpRestClient())(restHost);
Or extend using classical inheritance
var HttpRestClient = System.getModule( "com.simplygeek.rest" ).HttpRestClient(); ApiService.prototype = Object.create( HttpRestClient.prototype ); ApiService.prototype.constructor = ApiService;
Parameters:
Name | Type | Description |
---|---|---|
restHost | REST:RESTHost | The HTTP-REST RESTHost (either from the Inventory or transient) |
retryMaxAttempts | Number | OPTIONAL – The max number of times to retry the connection (defaults to 5) |
retryDelay | Number | OPTIONAL – The number of seconds between retries (defaults to 10) |
retryOn500 | Boolean | OPTIONAL – Whether to retry on a 500 status code (defaults to true) |
A new instance of the HttpRestClient class will be created and exposed by the variable ‘rest‘ that can be used to perform the required API calls. Note that ‘rest‘ can be changed to any value you require.
Supported Methods
To use HttpRestClient to perform an API call, use one of the following methods:
GET
rest.httpGet(uri, acceptType, expectedResponseCodes, headers) → {*}
Parameters:
Name | Type | Description |
---|---|---|
uri | String | The request uri |
acceptType | String | OPTIONAL – The Accept-Type media type (defaults to application/json) |
expectedResponseCodes | Array/Number | OPTIONAL – A list of expected response codes (defaults to [200, 201, 204]) |
headers | Array/String | OPTIONAL – A key/value set of headers to include in the request |
Returns the request response object.
POST
rest.httpPost(uri, acceptType, content, contentType, expectedResponseCodes, headers) → {*}
Parameters:
Name | Type | Description |
---|---|---|
uri | String | The request uri |
acceptType | String | OPTIONAL – The Accept-Type media type (defaults to application/json) |
content | Object | OPTIONAL – The request content (stringifies the payload when sent, defaults to {}) |
contentType | String | OPTIONAL – The Content-Type media type (defaults to application/json) |
expectedResponseCodes | Array/Number | OPTIONAL – A list of expected response codes (defaults to [200, 201, 204]) |
headers | Array/String | OPTIONAL – A key/value set of headers to include in the request |
Returns the request response object.
PUT
rest.httpPut(uri, acceptType, content, contentType, expectedResponseCodes, headers) → {*}
Parameters:
Name | Type | Description |
---|---|---|
uri | String | The request uri |
acceptType | String | OPTIONAL – The Accept-Type media type (defaults to application/json) |
content | Object | The request content (stringifies the payload when sent) |
contentType | String | OPTIONAL – The Content-Type media type (defaults to application/json) |
expectedResponseCodes | Array/Number | OPTIONAL – A list of expected response codes (defaults to [200, 201, 204]) |
headers | Array/String | OPTIONAL – A key/value set of headers to include in the request |
Returns the request response object.
PATCH
rest.httpPatch(uri, acceptType, content, contentType, expectedResponseCodes, headers) → {*}
Parameters:
Name | Type | Description |
---|---|---|
uri | String | The request uri |
acceptType | String | OPTIONAL – The Accept-Type media type (defaults to application/json) |
content | Object | The request content (stringifies the payload when sent) |
contentType | String | OPTIONAL – The Content-Type media type (defaults to application/json) |
expectedResponseCodes | Array/Number | OPTIONAL – A list of expected response codes (defaults to [200, 201, 204]) |
headers | Array/String | OPTIONAL – A key/value set of headers to include in the request |
Returns the request response object.
DELETE
rest.httpDelete(uri, acceptType, expectedResponseCodes, headers) → {*}
Parameters:
Name | Type | Description |
---|---|---|
uri | String | The request uri |
acceptType | String | OPTIONAL – The Accept-Type media type (defaults to application/json) |
expectedResponseCodes | Array/Number | OPTIONAL – A list of expected response codes (defaults to [200, 201, 204]) |
headers | Array/String | OPTIONAL – A key/value set of headers to include in the request |
Returns the request response object.
HEAD
rest.httpHead(uri, acceptType, expectedResponseCodes, headers) → {*}
Parameters:
Name | Type | Description |
---|---|---|
uri | String | The request uri |
acceptType | String | OPTIONAL – The Accept-Type media type (defaults to application/json) |
expectedResponseCodes | Array/Number | OPTIONAL – A list of expected response codes (defaults to [200, 201, 204]) |
headers | Array/String | OPTIONAL – A key/value set of headers to include in the request |
Returns the request response object.
Responses
Each method will return the RESTResponse object. I felt it would be easier to leave it to the developer to decide how to handle the response. This way, you can decide if you want the content in string format, retrieve headers, or both. Look at my examples to see how responses are handled.
Examples
The following are some examples of using the HttpRestClient.
GET Example
var rest = new (System.getModule("com.simplygeek.rest").HttpRestClient())(restHost); var mediaType = "application/json"; var uri = "/api/v2/tokens/"; var expectedResponseCodes = [200]; var response = rest.httpGet( uri, mediaType, expectedResponseCodes ); var responseContent = JSON.parse(response.contentAsString);
POST Example
var rest = new (System.getModule("com.simplygeek.rest").HttpRestClient())(restHost); var mediaType = "application/json"; var uri = "/api/v2/tokens/"; var expectedResponseCodes = [201]; var content = { application: applicationId, scope: scope }; var response = rest.httpPost( uri, mediaType, content, mediaType, expectedResponseCodes ); var responseContent = JSON.parse(response.contentAsString);
DELETE Example
var rest = new (System.getModule("com.simplygeek.rest").HttpRestClient())(restHost); var mediaType = "application/json"; var sessionId = "abcde"; var uri = "/api/v2/tokens/" + sessionId + "/"; var expectedResponseCodes = [204]; rest.httpDelete( uri, mediaType, expectedResponseCodes );
Thanks for reading, and please let me know if you have any suggestions for improving this service.