LaxmiRagi
Enthusiast
Enthusiast

Create Transient RESTHost and RESTOperation.

Jump to solution

Hi,

I am facing problem while creating transient RESThost and RestOperation.

Execute a transient RESTOperation for a transient RESTHost - Samples - VMware {code}

While executing above reference code as it is below error occurs

Unable to create object : RESTHost : com.vmware.o11n.plugin.rest.RESTHost

My Inputs:

URL : https://myhost/myapp/api/

user: username

password : pass2345

Please help me to understand where am i doing mistake.

Thnak you.

1 Solution

Accepted Solutions
philippbck
Contributor
Contributor

// Create REST Request

var restHost = RESTHostManager.createHost("dynamicRequest");

httpRestHost = RESTHostManager.createTransientHostFrom(restHost);

httpRestHost.operationTimeout = 60;

httpRestHost.connectionTimeout = 30;

httpRestHost.hostVerification = false;

httpRestHost.url = requestHostUrl;

var request = httpRestHost.createRequest(requestMethod, requestTemplateUrl, requestBody);

//  Basic authentication:

var authParams = ['Shared Session', username, password];

var authenticationObject = RESTAuthenticationManager.createAuthentication('Basic', authParams);

httpRestHost.authentication = authenticationObject;

// Set Content Type

request.contentType = requestContentType;

if (closeConnection == true){

request.setHeader("Connection", "Close")

}

// Execute REST Request

System.log("REST Request: " + requestMethod + " " + request.fullUrl);

var response = request.execute();

// Output

statusCodeAttribute = response.statusCode;

System.log("REST Response Status Code: " + statusCodeAttribute);

responseAsString = response.contentAsString;

if(logResponseOutput == true){

System.log("REST Response Content: " + responseAsString);

}

View solution in original post

4 Replies
eoinbyrne
Expert
Expert

When I used the Transient RESTHost mechanism I had to use an existing inventory RESTHost as the template

e.g.,

var myURL = "https://myhost/myapp/api";

var transientRESTHost = RESTHostManager.createTransientHostFrom(inventoryRESTHost);

transientRESTHost.url = myURL;

The class document for that method - vRO API Explorer by Dr Ruurd and Flores of ITQ - shows that an existing RESTHost instance is used as a template / prototype

HTH

0 Kudos
philippbck
Contributor
Contributor

// Create REST Request

var restHost = RESTHostManager.createHost("dynamicRequest");

httpRestHost = RESTHostManager.createTransientHostFrom(restHost);

httpRestHost.operationTimeout = 60;

httpRestHost.connectionTimeout = 30;

httpRestHost.hostVerification = false;

httpRestHost.url = requestHostUrl;

var request = httpRestHost.createRequest(requestMethod, requestTemplateUrl, requestBody);

//  Basic authentication:

var authParams = ['Shared Session', username, password];

var authenticationObject = RESTAuthenticationManager.createAuthentication('Basic', authParams);

httpRestHost.authentication = authenticationObject;

// Set Content Type

request.contentType = requestContentType;

if (closeConnection == true){

request.setHeader("Connection", "Close")

}

// Execute REST Request

System.log("REST Request: " + requestMethod + " " + request.fullUrl);

var response = request.execute();

// Output

statusCodeAttribute = response.statusCode;

System.log("REST Response Status Code: " + statusCodeAttribute);

responseAsString = response.contentAsString;

if(logResponseOutput == true){

System.log("REST Response Content: " + responseAsString);

}

LaxmiRagi
Enthusiast
Enthusiast

Thank you so much its working...! Smiley Happy

0 Kudos
ericm24
Contributor
Contributor

Hello,

I am facing similar situation, however, I need to use Cert-Based Auth, and send the cert that is on the current box.

Basically the the code below usese "basic auth".

var authParams = ["Shared Session", user, pw];

var authObject = RESTAuthenticationManager.createAuthentication("Basic", authParams);

What would be the parameters & setup to call .createAuthentication(..) and specify cert-based auth?

It looks like this is possible, because when you evoke the "Add a REST Host" action, you have the option to specify "The Private Key Entry For certificate based authentication", which allows me to select a cert that has been on-boarded to this machine.

Any help would be greatly appreciated.

0 Kudos