Empower VCF Automation Orchestrator API integration with HttpRestClient
VCF Automation 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 of using 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 Automation Orchestrator and the API endpoint.

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);
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.get(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.post(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 (stringify’s 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.put(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 (stringify’s 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.patch(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 (stringify’s 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.delete(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.head(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.get( 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.post( 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.delete( uri, mediaType, expectedResponseCodes );
Thanks for reading, and please let me know if you have any suggestions for improving this service.
We ended up with a similar REST client class, but via inheritance we created specialized classes handling a certain API (VCF Automation, Bitbucket, ServiceNow, etc.) These classes hide the implementation details (creating and passing Auth headers, payload differences) by adding additional functions and specialized constructors.
Hi Krisztián
That would be the good way to do it and is the reason I define prototypes, so that inheritance can be used. Thanks for sharing!