Javascript Example
Using Nodejs To Create a
vSphere
Automation API Session with SSO Credentials
vSphere
Automation
API Session with SSO CredentialsThis example shows
the use of Javascript with the
vSphere
Automation SDK for REST
to send a session creation request to the
using
credentials. The example retrieves a session cookie for
future authentication. The Javascript code depends on the Node.js package,
which allows it to run standalone.
This example depends on the
following global variables:
- my_vcsa_host
- my_sso_username
- my_sso_password
- my_http_options
For clarity, this example
specifies a complete set of HTTP options for the NodeJS request, rather than
retaining and modifying an existing object.
var https = require('https'); var httpPort = 443; var httpPath = '/rest/com/vmware/cis/session'; var httpMethod = 'POST'; // Prepare the HTTP request. my_http_options = { host: my_vcsa_host, port: httpPort, path: httpPath, method: httpMethod, rejectUnauthorized: false, requestCert: true, agent: false, auth: my_sso_username + ":" + my_sso_password }; // Define the callbacks. function callback(res) { console.log("STATUS: " + res.statusCode); res.on('error', function(err) { console.log("ERROR in SSO authentication: ", err) }); res.on('data', function(chunk) {}); res.on('end', function() { if (res.statusCode == 200) { // Save session ID authentication. var cookieValue = res.headers['set-cookie']; my_http_options.headers = {'Cookie': cookieValue}; // Remove username-password authentication. my_http_options.auth = {}; } console.log("Session ID:\n" + res.headers['set-cookie']); }; // Issue the session creation request. https.request(my_http_options, callback).end();