banner
Cypress

Cypress

twitter
telegram
github
discord server

Simple usage of the cpp-httplib library

Introduction to Cpp-httplib#

cpp-httplib is a single-header http library implemented in c++. The reason I came across this library is because our team's AI project recently needed to upload results from edge devices to the backend. In order to better integrate with the edge AI, I wanted to use c++ to complete this task. Project Address

Integrating cpp-httplib#

Method 1: Installation through vcpkg#

This requires installing vcpkg first, which I won't go into detail here.
After installing vcpkg, you can install cpp-httplib with the following command:

vcpkg install cpp-httplib:arm64-linux

Replace arm64-linux with the corresponding platform architecture of your device during installation.

Method 2: Directly integrating the header file#

Recommend

Download the httplib.h file directly to your local machine and place it in your project as needed.

Example of using the client#

Client construction#

httplib::Client cli("localhost");
httplib::Client cli("localhost:8080");
httplib::Client cli("http://localhost");
httplib::Client cli("http://localhost:8080");
httplib::Client cli("https://localhost");
httplib::SSLClient cli("localhost");

Get request without parameters#

#include "httplib.h"
#include <iostream>

int main(void)
{
  httplib::Client cli("localhost", 1234);

  if (auto res = cli.Get("/hi")) {
    if (res->status == 200) {
      std::cout << res->body << std::endl;
    }
  } else {
    auto err = res.error();
    std::cout << "HTTP error: " << httplib::to_string(err) << std::endl;
  }
}

cpp-httplib encapsulates the response body (res->body) as a std::string type. If you want to operate on its values more conveniently, consider using a json library to parse it.

Get request with headers#

httplib::Headers headers = {
  { "Accept-Encoding", "gzip, deflate" }
};
auto res = cli.Get("/hi", headers);

or

cli.set_default_headers({
  { "X-Access-Token", token }
});
auto res = cli.Get("/getUserList");

Get request with parameters#

std::stringstream ss;
ss << GetMillitimestamp();
std::string tStr = ss.str();
std::string sign = calculateSignature(appKey, appSecret, tStr);
httplib::Result res = cli.Get("/getToken?appKey=" + appKey + "&sign=" + sign + "&timestamp=" + tStr);

This is the simplest way to meet my requirements, but it also provides many APIs that can carry parameters.

inline Result Client::Get(const std::string &path, const Params &params,
                          const Headers &headers, Progress progress) {
  return cli_->Get(path, params, headers, progress);
}
inline Result Client::Get(const std::string &path, const Params &params,
                          const Headers &headers,
                          ContentReceiver content_receiver, Progress progress) {
  return cli_->Get(path, params, headers, content_receiver, progress);
}
inline Result Client::Get(const std::string &path, const Params &params,
                          const Headers &headers,
                          ResponseHandler response_handler,
                          ContentReceiver content_receiver, Progress progress) {
  return cli_->Get(path, params, headers, response_handler, content_receiver,
                   progress);
}

I don't know why the author didn't provide a simple API that carries parameters, like this:

inline Result Client::Get(const std::string &path, const Params &params) {
  return cli_->Get(path, params);
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.