STTNet 0.7.0

Demo: HTTP client

Buildable, runnable, verifiable, and synchronized with the repository source.

1. Example Scope

Key calls are explained with English comments, and the page includes run commands, validation steps, and adjustable settings.

2. Build and Run

cmake --build build --target sttnet_http_client --parallel
./build/examples/sttnet_http_client

3. Validation

Run an HTTP server on 127.0.0.1:8080, then start the client
Expected: A complete HTTP response

4. Complete Source

#include <sttnet.h>

#include <iostream>

int main()
{
    using stt::network::HttpClient;

    // HttpClient is a synchronous client. Use a finite timeout in production
    // and run it outside an STTNet Reactor callback.
    HttpClient client;

    if(!client.getRequest("http://127.0.0.1:8080/ping", "",
                          "Connection: close", 5))
    {
        std::cerr << "request could not be sent\n";
        return 1;
    }

    // Sending successfully is not the same as receiving a complete response.
    if(!client.isReturn())
    {
        std::cerr << "server did not return a complete HTTP response\n";
        return 2;
    }

    std::cout << "--- response headers ---\n" << client.header
              << "\n--- response body ---\n" << client.body << '\n';
    return 0;
}