HTTP author curl -u support
lioncruise opened this issue · 4 comments
I want to write a C++ HTTP client to query ES's RESTful API, but ES has user and password, I do it with curl -u user:password http://xxxx.
~ curl http://10.130.148.131:9200/_cluster/health
{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication token for REST request [/_cluster/health]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication token for REST request [/_cluster/health]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}[shijunqin@10-165-15-4 ~]#
~ curl -u [user]:[password] http://10.130.148.131:9200/_cluster/health
{"cluster_name":"es-h7jpiub2","status":"green","timed_out":false,"number_of_nodes":9,"number_of_data_nodes":6,"active_primary_shards":412,"active_shards":424,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":100.0}
You must send the Authorization
header, like this: Authorization: Basic am9objpwd2Q=
where dXNlcjpwYXNzd29yZA==
is base64-encoded string "user:password". Use can use https://www.base64encode.org to encode your user and password with base64.
You must send the
Authorization
header, like this:Authorization: Basic am9objpwd2Q=
wheredXNlcjpwYXNzd29yZA==
is base64-encoded string "user:password". Use can use https://www.base64encode.org to encode your user and password with base64.
@elvissstrazdins Thank you very much for your help!
@elvissstrazdins using your tip above, i wrote the following code:
std::vector<std::string> headers;
headers.push_back("Authorization: Basic ZWxhc3RpYzptaW5pVmlkZW9AMTIzNDU2");
const auto response = request.send("GET", "", headers);
std::cout << std::string{response.body.begin(), response.body.end()} << '\n';
the request stuck at this code in your include/HTTPRequest.hpp
file, while I can request successfully with curl -u
with the same HTTP auth and query url. I'm not familiar with network programming. what might be the problem?
auto count = ::select(endpoint + 1,
(type == SelectType::read) ? &descriptorSet : nullptr,
(type == SelectType::write) ? &descriptorSet : nullptr,
nullptr,
(timeout >= 0) ? &selectTimeout : nullptr);