Python版本:
import requests
import json
session = requests.post("https://172.17.xx.xx/rest/com/vmware/cis/session", auth=('administrator@xx.xx','xxxxx!@#'), verify=False)
session_id = session.json()['value']
response = requests.get('https://172.17.150.15/rest/vcenter/host', verify=False, headers={
'vmware-api-session-id':session_id
})
# print(response.json)
# print(u'response.text= %s' %str(response.text))
json_data = json.loads(response.text)
# print(json_data['value'][len(json_data['value']) -1])
print(json_data)
nodejs版本:
const https = require('https');
const options = {
hostname: 'vc22.xx.xx',
port: 443,
path: '/rest/com/vmware/cis/session',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
auth: 'administrator@xx.xx:xxxxx',
rejectUnauthorized: false
};
const req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (e) => {
console.error(e);
});
req.end();
返回的结果示例:
statusCode: 200
headers: {
date: 'Thu, 11 Jun 2020 03:16:00 GMT',
'set-cookie': [
'vmware-api-session-id=3928916724977c87fa93b0058cdf6f4b;Path=/rest;Secure;HttpOnly'
],
expires: 'Thu, 01 Jan 1970 00:00:00 GMT',
'content-type': 'application/json',
connection: 'close'
}
{"value":"3928916724977c87fa93b0058cdf6f4b"}