Kurlyk
Loading...
Searching...
No Matches
nested_http_requests_example.cpp
Go to the documentation of this file.
1#include <kurlyk.hpp>
2#include <iostream>
3
4// Thread-safe logging of response details
7 << "Response received:" << std::endl
8 << "Ready: " << response->ready << std::endl
9 << "Content: " << response->content << std::endl
10 << "Error Code: " << response->error_code << std::endl
11 << "Status Code: " << response->status_code << std::endl
12 << "----------------------------------------" << std::endl;
13}
14
15int main() {
16 kurlyk::init(true);
17
18 // Initialize the HTTP client
19 kurlyk::HttpClient client("https://httpbin.org");
20 client.set_user_agent("KurlykClient/1.0");
21 client.set_verbose(true);
22
23 // Send the first GET request
24 KURLYK_PRINT << "Sending the first GET request..." << std::endl;
25 client.get("/ip", kurlyk::QueryParams(), kurlyk::Headers(),
26 [&client](const kurlyk::HttpResponsePtr response) {
27 print_response(response);
28
29 if (response->ready && response->status_code == 200) {
30 KURLYK_PRINT << "First request succeeded. Sending the second request..." << std::endl;
31
32 // Send a second GET request within the callback of the first request
33 client.get("/user-agent", kurlyk::QueryParams(), kurlyk::Headers(),
34 [](const kurlyk::HttpResponsePtr response) {
35 KURLYK_PRINT << "Second request completed." << std::endl;
36 print_response(response);
37 });
38 } else {
39 KURLYK_PRINT << "First request failed. Not sending the second request." << std::endl;
40 }
41 });
42
43 KURLYK_PRINT << "Press Enter to exit..." << std::endl;
44 std::cin.get();
45 client.cancel_requests();
47 return 0;
48}
A client class for making HTTP requests to a specific host.
void set_verbose(bool verbose)
Enables or disables verbose output.
void set_user_agent(const std::string &user_agent)
Sets the User-Agent header.
bool get(const std::string &path, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends a GET request.
void cancel_requests()
Cancels the active request associated with this client and waits for its completion.
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 the network worker and releasing resources.
Definition runtime.hpp:26
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
void print_response(const kurlyk::HttpResponsePtr &response)
#define KURLYK_PRINT