pistacheio / pistache

A high-performance REST toolkit written in C++
https://pistacheio.github.io/pistache/
Apache License 2.0
3.19k stars 699 forks source link

[Question] How to create client using https or TLS(or SSL)? #969

Open iAmSKU opened 3 years ago

iAmSKU commented 3 years ago

I am using pistache http client to communicate with golang server, earlier it was implemented using the http protocol.

Now because of the security concern I want to switch to the https/TLS client.

I have looked on the code base and unable to found a way to use Http::Client (Referred https://github.com/pistacheio/pistache/pull/226, but it is for server).

Following is the snippet of code I am using:

std::string sendPostRequest(const std::string & query, const std::string & inData) {
        std::cout << "sendPostRequest" << std::endl;

        Http::Client client;
        //auto client_opts = Http::Client::options().threads(3).maxConnectionsPerHost(3);
        //client.init(client_opts);
        client.init();
        //client.useSSL("certificate.pem", "key.pem"); //No such interface for client

        auto start = std::chrono::system_clock::now();

        std::cout << "sendPostRequest#1" << std::endl;
        auto rb = client.post(query)
                        .body(inData);
                        //.timeout(std::chrono::seconds(10))
                        //.send();

        auto response = rb.header<Http::Header::Connection>(
                        Http::ConnectionControl::KeepAlive).send();

        std::string data = "";
        response.then([&data](Http::Response rsp) {
                stringstream ss;
                ss << rsp.code();
                if (rsp.code() == Http::Code::Ok)
                {
                        auto body = rsp.body();
                        if (!body.empty()) {
                                std::cout << "Response body = " << body << std::endl;
                                data = body;
                        }
                }
        }, Async::IgnoreException);

        Async::Barrier<Http::Response> barrier(response);
        barrier.wait_for(std::chrono::seconds(5));

        auto end = std::chrono::system_clock::now();

        std::cout << "Total time of execution:" << std::chrono::duration_cast
                        < std::chrono::milliseconds
                        > (end - start).count() << "ms" << std::endl;

        client.shutdown();

        return data;
}
NemanjaL92 commented 2 years ago

Does pistache client support SSL?