Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP author curl -u support #42

Closed
lioncruise opened this issue Jun 21, 2021 · 4 comments
Closed

HTTP author curl -u support #42

lioncruise opened this issue Jun 21, 2021 · 4 comments

Comments

@lioncruise
Copy link

lioncruise commented Jun 21, 2021

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}
@lioncruise lioncruise changed the title curl -u support HTTP author curl -u support Jun 21, 2021
@elnormous
Copy link
Owner

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.

@lioncruise
Copy link
Author

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.

@elvissstrazdins Thank you very much for your help!

@lioncruise
Copy link
Author

lioncruise commented Jun 23, 2021

@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);

@elnormous
Copy link
Owner

HTTPRequest did not pass the port number in the request. This was fixed in cdaa3cd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants