Installation and the First Request¶
Contents | Previous: HTTP Client Overview | Next: Making Requests
View in another language — C++ · C#/.NET · Java · Kotlin · Node/TypeScript
After reading this chapter
You can add the HTTP client package to a project, receive a typed GET response through one client, and choose a one-shot for a single request.
The HTTP client ships separately from the server framework. A calling process needs only the HTTP client package. The tutorial below creates a client and reads one profile.
1. Package Reference¶
find_package(zlink_http_client_cpp CONFIG REQUIRED)
target_link_libraries(my_client PRIVATE zlink::http_client)
Each tab shows where that language references the package. The Java and Kotlin tabs quote the tutorial's actual Gradle dependencies, while Node/TypeScript installs the public npm package.
2. Client Creation and the First GET Request¶
A builder holding the base URL and default options creates a client. A request builder is created when a method such as GET or POST is selected from the client. A typed response terminator decodes the JSON body to the requested type and returns the response envelope. An asynchronous terminator does not occupy a thread or event loop while it waits for the result.
// The client is shared by the sequential examples and destroyed at program exit.
auto client = hc::client_t::create ("http://127.0.0.1:5180")
.timeout (std::chrono::seconds (3))
.build ();
// The CLI awaits the asynchronous terminator before printing its response.
const auto first = co_await client.get ("/players/p1/profile").async<player_profile_t> ();
std::cout << "first request: " << first.body.player_id << " " << first.body.nickname
<< std::endl;
The code creates a client with a three-second default timeout, sends a profile GET request, and prints the player ID and nickname from the typed response body.
3. Result¶
The output below comes from the tutorial (framework/languages/cpp/tutorial) HttpClient with its Server and Client started as described in the README's "Run" section; the address is the Client's HTTP surface.
The captured runs for four languages show the same player ID and nickname. The C++ result remains reserved for the #714 fix.
4. A Single Request¶
A one-shot obtains its request builder directly from the client builder. It closes the client after completion, so it does not reuse a connection pool. A service that calls the same API repeatedly keeps a client as in the preceding section.
Next Chapter¶
Making Requests sets methods, paths, queries, headers, and per-request timeouts. Handling Responses chooses a response form, and Client and Request Lifecycle covers reuse and closing.