vRealize Orchestrator: HTTP-REST Client

vRealize Orchestrator provides a nice way to manage and interact with web services. I have always liked that you can add HTTP-REST API endpoints, configured with basic authentication, which doesn’t require a token to be requested each time they are used. It’s also quite useful having visibility of these endpoints in the inventory, which makes them easy to discover.

It’s also possible to create operations on these endpoints (i.e. all the get/post operations), but I am not a big fan of using these. The problem, is that using them takes you that little bit further away from managing everything in code. They are also more difficult to port/migrate to other vRO instances, which is especially true if you have multiple environments for development, test, pre-prod, prod, etc. My much preferred method is to use Actions which perform these operations instead.

I have also seen many other developers use Actions for their HTTP-REST operations, which is good. The problem, however, is that I have seen these written in so many different ways. I decided that I needed to create a consistent experience when interacting with these web services and created the HTTP Rest Client. This client is a class with associated methods, that are used to make requests against a HTTP-REST web services.

You can download a package containing the HTTP-REST Client Action here.

HTTP REST Client

The client is an action that returns the HttpClient object. This object has a number of methods that can be used to execute requests against a web service. The following methods are supported:

  • GET, POST, PUT, DELETE and PATCH

The Action itself requires no inputs as these are provided when instantiating the object or supplied to the methods, when called.

Below is the code for the HttpClient class.

Usage

A new instance of the HttpClient class can be created using the new keyword. The class allows two parameters to be provided:

new HttpClient(restHost, acceptType)

Parameters:

Name Type Description
restHost REST:RESTHost The HTTP REST host.
acceptType string (optional) The encoding format to accept.

To use the HTTP-REST Client, simply add the following lines of code into an Action:

 

The following methods can be used:

GET

get(restUri, expectedResponseCodes, headers) → {*}

Parameters:

Name Type Description
restUri string The request uri.
expectedResponseCodes Array/number (optional) A list of expected response codes.
headers Properties (optional) A key/value set of headers to include in the request.

Returns the request response object.

 

POST

post(restUri, content, contentType, expectedResponseCodes, headers) → {*}

Parameters:

Name Type Description
restUri string The request uri.
content string (optional) The request content.
contentType string (optional) The encoding for content.
expectedResponseCodes Array/number (optional) A list of expected response codes.
headers Properties (optional) A key/value set of headers to include in the request.

Returns the request response object.

 

PUT

put(restUri, content, contentType, expectedResponseCodes, headers) → {*}

Parameters:

Name Type Description
restUri string The request uri.
content string The request content.
contentType string (optional) The encoding for content.
expectedResponseCodes Array/number (optional) A list of expected response codes.
headers Properties (optional) A key/value set of headers to include in the request.

Returns the request response object.

 

DELETE

delete(restUri, content, contentType, expectedResponseCodes, headers) → {*}

Parameters:

Name Type Description
restUri string The request uri.
content string The request content.
contentType string (optional) The encoding for content.
expectedResponseCodes Array/number (optional) A list of expected response codes.
headers Properties (optional) A key/value set of headers to include in the request.

Returns the request response object.

 

PATCH

patch(restUri, content, contentTypeopt, expectedResponseCodesopt, headersopt) → {*}

Parameters:

Name Type Description
restUri string The request uri.
content string The request content.
contentType string (optional) The encoding for content.
expectedResponseCodes Array/number (optional) A list of expected response codes.
headers Properties (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 decided if you want the content in string format, or retrieve headers, or both. Look at my examples to see how responses are handled.

Examples

I have provided some examples which were used with some NSX API interactions to demonstrate the use of the HTTP-REST Client.

GET Example

POST Example

PUT Example

DELETE Example

I hope this has been useful. If you discover bugs with any of my code, require some help or simply need an ad-hoc solution, then please drop me a message via the Drift app.

Please rate this post!

Average rating / 5. Vote count:

Leave a Reply

avatar
  Subscribe  
Notify of