我们正在编写一个 C++ 程序来集成到 VMware vCenter。我们正在使用其余的 API 连接失败且未找到,因此我们创建了一个单独的测试程序。
我们添加了很多日志记录,会话似乎正在连接但不接受凭据。
bool generateSessionID(std::string &sessionID) {
CURL *curl;
CURLcode res = CURLE_OK;
std::string response;
std::string hostname = "192.168.0.223"; // Hardcoded vCenter IP address
std::string username = "[email protected]"; // Hardcoded username
std::string password = "Password1"; // Hardcoded password
curl = curl_easy_init();
if (curl) {
std::string url = "https://" + hostname + "/rest/com/vmware/cis/session";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
std::cout << "HTTP response code: " << http_code << std::endl;
std::cout << "Response: " << response << std::endl;
if (http_code == 200) {
Json::CharReaderBuilder builder;
Json::CharReader *reader = builder.newCharReader();
Json::Value jsonObj;
std::string errs;
if (reader->parse(response.c_str(), response.c_str() + response.size(), &jsonObj, &errs)) {
sessionID = jsonObj["value"].asString();
std::cout << "Session ID: " << sessionID << std::endl;
} else {
std::cerr << "Failed to parse JSON: " << errs << std::endl;
}
delete reader;
} else {
std::cerr << "Error: HTTP response code " << http_code << std::endl;
}
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
return res == CURLE_OK;
}
This is the error message we get
HTTP response code: 404
Response: {"majorErrorCode":404,"name":"com.vmware.vapi.rest.httpNotFound","localizableMessages":[{"args": [],"defaultMessage":"Not found.","id":"com.vmware.vapi.rest.httpNotFound"}],"type":"com.vmware.vapi.std.errors.not_found","value":{"messages":[{"args":[],"default_message":"Not found.","id":"com.vmware.vapi.rest.httpNotFound"}]}}
Error: HTTP response code 404
我们在 C++ 代码中使用与从命令行测试时相同的端点,但收到未找到错误。
我们已使用以下命令从命令行成功测试了连接性
http --verify=no -a '[email protected]:Password1' POST https://192.168.0.223/rest/com/vmware/cis/session
HTTP/1.1 200 OK
content-type: application/json
date: Fri, 14 Jun 2024 13:20:32 GMT
expires: Thu, 01 Jan 1970 00:00:00 GMT
set-cookie: vmware-api-session-id=3e507a19d9653a97a6b520e9020a9aee; Path=/rest; Secure; HttpOnly, vmware-api-session-id=3e507a19d9653a97a6b520e9020a9aee; Path=/api; Secure; HttpOnly
transfer-encoding: chunked
x-envoy-upstream-service-time: 195
{
"value": "3e507a19d9653a97a6b520e9020a9aee"
}
使用卷曲
curl -k -u '[email protected]:Password1' -X POST https://192.168.0.223/rest/com/vmware/cis/session
{"value":"0f9b655ee47584b4493c27c38a99fa55"}
我能看到的唯一区别 - libcurl 执行 GET 请求,其他尝试执行 POST 请求。
您可能想添加
curl_easy_setopt(curl, CURLOPT_POST, 1L);