Kurlyk
Loading...
Searching...
No Matches
http_streaming_example.cpp
Go to the documentation of this file.
1#define KURLYK_AUTO_INIT 0
2#include <kurlyk.hpp>
3
4#include <atomic>
5#include <chrono>
6#include <future>
7#include <iostream>
8
9int main() {
10 kurlyk::init(true);
11
12 std::promise<void> done_promise;
13 auto done_future = done_promise.get_future();
14 std::atomic<int> chunk_count(0);
15
16 const uint64_t request_id = kurlyk::http_get(
17 "http://httpbin.org/stream/5",
20 true,
21 [&done_promise, &chunk_count](kurlyk::HttpResponsePtr response) {
22 if (!response) return;
23
24 if (response->stream_chunk) {
25 ++chunk_count;
26 KURLYK_PRINT << "chunk: " << response->content << std::endl;
27 return;
28 }
29 if (!response->ready) return;
30
32 << "ready: " << std::boolalpha << response->ready << std::endl
33 << "status_code: " << response->status_code << std::endl
34 << "chunks: " << chunk_count.load() << std::endl
35 << "error_code: " << response->error_code.message() << std::endl;
36
37 try {
38 done_promise.set_value();
39 } catch (...) {
40 }
41 });
42
43 KURLYK_PRINT << "Request id: " << request_id << std::endl;
44 done_future.wait_for(std::chrono::seconds(30));
45
47 return 0;
48}
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
Definition runtime.hpp:13
std::unique_ptr< HttpResponse > HttpResponsePtr
A unique pointer to an HttpResponse object for memory management.
void deinit()
Deinitializes the Kurlyk library, stopping async processing or cleaning up synchronous state.
Definition runtime.hpp:26
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
uint64_t http_get(const std::string &url, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends an asynchronous HTTP GET request with a callback.
Definition utils.hpp:362
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
#define KURLYK_PRINT