cofetch
Chainable, high-performance async HTTP client for C++ event loops
Loading...
Searching...
No Matches
example_fluent.cpp

Building a POST with the chainable setters, finished by the verb.

Building a POST with the chainable setters, finished by the verb.

// Build a request as a chain: setters flow off request(), and the HTTP
// verb at the end fires the transfer.
#include <cofetch.h>
#include <asio.hpp>
#include <chrono>
#include <iostream>
int main() {
asio::io_context io;
cofetch::Client http(io);
http.request("https://postman-echo.com/post")
.headers({"content-type: application/json"})
.body(R"({"greeting": "hello cofetch"})")
.timeout(std::chrono::seconds(2))
.post([](std::error_code ec, const cofetch::Response& res) {
if (ec) {
std::cerr << "transport error: " << ec.message() << "\n";
return;
}
std::cout << "http " << res.http_code_ << "\n" << res.data_ << "\n";
});
io.run();
}
RequestBuilder & headers(std::vector< std::string > h)
Definition cofetch.h:350
auto post(CompletionToken &&token)
Definition cofetch.h:377
RequestBuilder & timeout(std::chrono::milliseconds t)
Definition cofetch.h:358
Definition cofetch.h:266
RequestBuilder request(std::string url)
Start a fluent request chain: request(url).body(...).post(token).
Definition cofetch.h:412
Definition cofetch.h:111
long http_code_
Definition cofetch.h:176
std::string data_
Definition cofetch.h:177