microsoft / cpprestsdk

The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Other
7.96k stars 1.65k forks source link

How to enable verbose log for cpprestsdk? #1663

Open hcoona opened 2 years ago

hcoona commented 2 years ago

I'm requesting an API via cpprestsdk. The API works fine with cURL commandline tool, but failed to work with cpprestsdk. How could I debug it? Where could I found the cpprestsdk log?

Unknown exception thrown. what=Failed to read HTTP status line
curl --location --request POST 'http://gemini-test.byted.org/api/privilege/default/grantPriToUser' \
  --header 'Authorization: xxxxxx' \
  --header 'app: hive' \
  --header 'security: 123' \
  --header 'token: 123' \
  --header 'time: 123' \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "datasource": "hive",
    "username": "zhujiang.97",
    "database": "hive_test",
    "table": "hive_test",
    "column": "",
    "priType": "SELECT",
    "expirationTime":-1 
}'
Status GeminiClientCppRestSdkImpl::Impl::PrepareGrantPriToUserOrPsmRequest(
    const GeminiClusterAndTopic& cluster_and_topic, GrantType grant_type,
    const std::string& user_or_psm,
    const absl::optional<absl::Time>& expireTime,
    web::http::http_request* request) const {
  static constexpr char kNoMeaningButRequiredHeaderValue[] = "123";

  request->set_method(web::http::methods::POST);
  request->headers()[web::http::header_names::authorization] = options_.token;
  request->headers()[web::http::header_names::accept] =
      web::http::details::mime_types::application_json;
  request->headers()[web::http::header_names::accept_charset] =
      web::http::details::charset_types::utf8;
  request->headers()[web::http::header_names::content_type] =
      web::http::details::mime_types::application_json;
  request->headers()[web::http::header_names::content_encoding] =
      web::http::details::charset_types::utf8;

  request->headers()["app"] = kNoMeaningButRequiredHeaderValue;
  request->headers()["time"] = kNoMeaningButRequiredHeaderValue;
  request->headers()["token"] = kNoMeaningButRequiredHeaderValue;
  request->headers()["security"] = kNoMeaningButRequiredHeaderValue;

  web::json::value value = web::json::value::object();
  value.as_object()["datasource"] = web::json::value::string("hive");
  if (grant_type == GrantType::kUser) {
    request->set_request_uri(
        web::uri_builder(absl::StrCat("/api/privilege/", options_.location,
                                      "/grantPriToUser"))
            .to_uri());
    value.as_object()["username"] = web::json::value::string(user_or_psm);
  } else if (grant_type == GrantType::kPsm) {
    request->set_request_uri(
        web::uri_builder(absl::StrCat("/api/privilege/", options_.location,
                                      "/grantPriToPSM"))
            .to_uri());
    value.as_object()["psmName"] = web::json::value::string(user_or_psm);
  } else {
    LOG(FATAL) << "Unimplemented grant_type: "
               << static_cast<std::underlying_type<GrantType>::type>(
                      grant_type);
  }

  value.as_object()["database"] = web::json::value::string(
      absl::StrCat(cluster_and_topic.prefixed_cluster_id));
  value.as_object()["table"] =
      web::json::value::string(absl::StrCat(cluster_and_topic.topic_name));
  value.as_object()["column"] = web::json::value::string("");
  value.as_object()["priType"] = web::json::value::string("SELECT");
  if (expireTime.has_value()) {
    value.as_object()["expirationTime"] =
        web::json::value::number(absl::ToUnixMillis(expireTime.value()));
  } else {
    value.as_object()["expirationTime"] = web::json::value::number(-1);
  }

  request->set_body(value);

  return OkStatus();
}
hcoona commented 2 years ago

The problem solved. It's because connection timeout. But still want to know if there was any way to enable verbose log, just like the -v flags for cURL.