belle

An HTTP / Websocket library in C++17 using Boost.Beast and Boost.ASIO.


belle

/

example

/

client

/

http

/

src

/

main.cc

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
// belle client http example

#include "belle.hh"
namespace Belle = OB::Belle;

#include <string>
#include <iostream>

// prototypes
void on_http_error(Belle::Client& app);
void http_get();
void http_post();

void on_http_error(Belle::Client& app)
{
  // set the http on error callback
  app.on_http_error([](auto& ctx)
  {
    std::cerr << "Error: " << ctx.ec.message() << "\n\n";
  });
}

void http_get()
{
  // init client with remote address and port
  Belle::Client app {"127.0.0.1", 8080};
  on_http_error(app);

  // http get request to path '/'
  app.on_http("/", [](auto& ctx)
  {
    // check http status code
    if (ctx.res.result() != Belle::Status::ok)
    {
      // print the response status code and reason
      std::cerr
      << "Error: " << ctx.res.result_int()
      << " " << ctx.res.reason() << "\n\n";

      return;
    }

    // print the response headers and body
    std::cerr << ctx.res.base() << ctx.res.body() << "\n\n";
  });

  // http get request to path '/method'
  app.on_http("/method", [](auto& ctx)
  {
    // check http status code
    if (ctx.res.result() != Belle::Status::ok)
    {
      // print the response status code and reason
      std::cerr
      << "Error: " << ctx.res.result_int()
      << " " << ctx.res.reason() << "\n\n";

      return;
    }

    // print the response headers and body
    std::cerr << ctx.res.base() << ctx.res.body() << "\n\n";
  });

  // init an http request object
  Belle::Request req;

  // set the target path
  req.target("/regex/hello");

  // set the method
  req.method(Belle::Method::get);

  // copy the request object
  app.on_http(req, [](auto& ctx)
  {
    // check http status code
    if (ctx.res.result() != Belle::Status::ok)
    {
      // print the response status code and reason
      std::cerr
      << "Error: " << ctx.res.result_int()
      << " " << ctx.res.reason() << "\n\n";

      return;
    }

    // print the full response
    std::cerr << ctx.res << "\n\n";
  });

  // set the target path
  req.target("/params");

  // set the query parameters
  req.params().emplace("pi", "π");
  req.params().emplace("page", "2");
  req.params().emplace("user", "François");
  req.params().emplace("q", "Hello,\nBelle!");
  req.params().emplace("q", "t#st spec!&l ch@r*ct=rs");

  // move the request object with Request::move
  // can also be moved using std::move
  app.on_http(req.move(), [](auto& ctx)
  {
    // check http status code
    if (ctx.res.result() != Belle::Status::ok)
    {
      // print the response status code and reason
      std::cerr
      << "Error: " << ctx.res.result_int()
      << " " << ctx.res.reason() << "\n\n";

      return;
    }

    // print the full response
    std::cerr << ctx.res << "\n\n";
  });

  // save the number of requests in the queue
  auto total = app.queue().size();

  // start the client and save the number of completed requests
  auto completed = app.connect();

  // print the number of completed requests
  std::cerr << "connect: " << completed << "/" << total << "\n\n";
}

void http_post()
{
  // init client with remote address and port
  Belle::Client app {"127.0.0.1", 8080};
  on_http_error(app);

  // set http headers
  Belle::Headers headers;
  headers.set(Belle::Header::user_agent, "Belle");
  headers.set(Belle::Header::content_type, "text/plain");

  // request parameters and response handler
  app.on_http(Belle::Method::post, "/post", headers, "Hello, Belle!", [](auto& ctx)
  {
    // check http status code
    if (ctx.res.result() != Belle::Status::ok)
    {
      // print the response status code and reason
      std::cerr
      << "Error: " << ctx.res.result_int()
      << " " << ctx.res.reason() << "\n\n";

      return;
    }

    // print the response body
    std::cerr << ctx.res.body() << "\n\n";
  });

  // save the number of requests in the queue
  auto total = app.queue().size();

  // start the client and save the number of completed requests
  auto completed = app.connect();

  // print the number of completed requests
  std::cerr << "connect: " << completed << "/" << total << "\n\n";
}

int main(int argc, char *argv[])
{
  // perform multiple http get requests to a single remote endpoint
  http_get();

  // perform an http post request to a single remote endpoint
  http_post();

  return 0;
}
Back to Top