/**
* @module de.stschnell.restRequestExample
*
* @version 0.1.0
*
* @param {string} in_vcoFQDN vCO FQDN
* @param {string} in_userName Name of the username
* @param {SecureString} in_password Password of the user
*
* @outputType Properties
*
* @description Example how to use a REST Request with JavaScript
*/
var restHost = RESTHostManager.createTransientHostFrom(
RESTHostManager.createHost("dynamicRequest")
);
restHost.url = in_vcoFQDN;
// Get the token for API call
var login = JSON.stringify({
"username": in_username,
"password": in_password
});
var request = restHost.createRequest(
"POST", "/csp/gateway/am/api/login?access_token", login
);
request.contentType = "application/json";
var response = request.execute();
var refreshToken = "{\"refreshToken\":\"" +
JSON.parse(response.contentAsString).refresh_token + "\"}";
var request = restHost.createRequest(
"POST", "/iaas/api/login", refreshToken
);
request.contentType = "application/json";
var response = request.execute();
var bearerToken = JSON.parse(response.contentAsString).token;
// As example here get all deployments of a project via
// Service Broker API call
var request = restHost.createRequest(
"GET", "/deployment/api/deployments?size=999&" +
"$filter=projectId%20eq%20%27f77f5d6e-ba20-469b-b56a-172c97e2c1f7%27"
);
request.setHeader("Content-Type", "application/json");
request.setHeader("Accept", "application/json");
request.setHeader("Authorization", "Bearer " + bearerToken);
var response = request.execute();
System.log(response.contentAsString);
return JSON.parse(response.contentAsString);
|