CPPREST credentials

22 Views Asked by At

I would like to use basic authentication in my server-client application.

On client i set credentials in client:

  credentials creds(
      utility::conversions::to_string_t(username),
      utility::conversions::to_string_t(password));
  http_client_config config;
  config.set_credentials(creds);

  m_client.reset(new http_client(host, config));

And make request to server

m_client->request(mtd, uri.to_string())
    .then([](http_response response) {
    // Check the status code
    if (response.status_code() == status_codes::OK) {
      return response.extract_json();
    }
    // Handle error or return an empty JSON in case of non-OK response
    return pplx::task_from_result(json::value());
      })
    .then([this, callback](pplx::task<json::value> previousTask) {
        try {
          const json::value &v = previousTask.get();
          callback(v, this);
        }
        catch (const http_exception &/*e*/) {
        }
        catch (const std::exception &/*e*/) {
        }
      })
        .wait();

But i don't understand where i can find this information on server part:

    m_pListener.reset(new http_listener(addr, config));

    auto handleGet = [this](http_request request)->void
    {
      // Check if the request URI is the expected endpoint
      auto path = request.request_uri().path();

      std::string endpoint;
      CommonFramework::WStringToString(path, endpoint);
      auto callback = m_pParent->GetCallback(Method::GetId, endpoint);
      if (callback)
      {
        std::unique_ptr<JSONSerilizer> serializer;
        serializer.reset(CommonFramework::MakeSerializer(false));
        callback->Do(serializer.get());
        request.reply(status_codes::OK, serializer->GetJsonValue());
      }
      else {
        // Reply with 404 Not Found if the endpoint is not as expected
        request.reply(status_codes::NotFound, U("Not Found"));
      }
    };

    m_pListener->support(methods::GET, handleGet);

there is not this information in request. I guess I don't get something

0

There are 0 best solutions below