0
//Basic Authorization Setting
http_client_config config;
credentials creds(L"name", L"password");
config.set_credentials(creds);
http_client client(U("http://severip/"), config);

// create header
http_request req(methods::GET);

//// Add base64 result to header
req.headers().add(L"Authorization", L"Basic bmFtZTpwYXNzd29yZA");
req.set_request_uri(L"/_cat/master");
pplx::task<http_response> responses = client.request(req);
pplx::task<web::json::value> jvalue = responses.get().extract_json();
web::json::value v = jvalue.get();
utility::string_t jsonval = v.serialize();
wcout << jsonval << endl;

//with base64 encoding.
//bmFtZTpwYXNzd29yZA = name:password

I'm trying to set a credentials through cpprestsdk aka casablanca.

And I've recived 401 error which is failed to athenticate user, it means password error.

Any advice would be thankful.

1 Answers1

0

Try to use the curl command to validate user/password quickly.


curl -X PUT --basic  -u {user}:{password} http://severip/

YouXiang-Wang
  • 1,119
  • 6
  • 15
  • thanks for comments. I know the user/password information. The problem is, my user&password are correct and curl command doing fine, but it spits out 401 error through cpprest-sdk. I think it's from the code problem. Any thoughts? – IC1101Galaxy Feb 05 '20 at 01:50
  • curl -H 'Authorization:Basic bmFtZTpwYXNzd29yZA' -H 'Content-Type: application/json' verify if the base 64 is correct or not. – YouXiang-Wang Feb 05 '20 at 01:59
  • I've got curl: (6) Could not resolve host: bmFtZTpwYXNzd29yZA curl: (6) Could not resolve host: application Does it means my base64 string is not correct? – IC1101Galaxy Feb 05 '20 at 02:05
  • curl http://${severip}/${port}/_cat/master -H 'Authorization:Basic bmFtZTpwYXNzd29yZA' -H 'Content-Type: application/json' – YouXiang-Wang Feb 05 '20 at 02:09
  • {"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/_cat/master]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication credentials for REST request [/_cat/master]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}curl: (6) Could not resolve host: bmFtZTpwYXNzd29yZA' curl: (6) Could not resolve host: application – IC1101Galaxy Feb 05 '20 at 02:13
  • No. The error is thrown by elastic-search cluster which I want to sending a query through cpprest-sdk – IC1101Galaxy Feb 05 '20 at 02:19
  • I got my cluster locked with password so I must enter the username and password to connect the cluster. – IC1101Galaxy Feb 05 '20 at 02:22
  • Yes. So if I want to sending a query like this one "curl -XGET http://{serverip:9200}/_cat/master" , I must add athentification option like "curl -XGET http://{serverip:9200}/_cat/master -u name:password" but in elastic-search, name will always static {elastic}. – IC1101Galaxy Feb 05 '20 at 02:52
  • What I want to do is just doing this curl job with athentification {-u elastic:password} through cpprest-sdk. Sending a query with no athentification is just worked fine with no problem. – IC1101Galaxy Feb 05 '20 at 02:56