NNG_HTTP_CLIENT_CONNECT(3http)

πŸ“¦ version 1.11, in alpine 3.24

πŸ’… nng_http_client_connect β€” say hi to your new server bestie ✨

okay so this is page NNG_HTTP_CLIENT_CONNECT(3http) from the NNG Reference Manual, and honestly? it's a little baddie of a function. let's get into it πŸŽ€


🌸 NAME 🌸

nng_http_client_connect β€” establish HTTP client connection πŸ¦‹


✨ SYNOPSIS ✨

#include <nng/nng.h>
#include <nng/supplemental/http/http.h>

void nng_http_client_connect(nng_http_client *client, nng_aio *aio);

like, that's it. that's the whole vibe. two includes and one line πŸ’œ


πŸ’– DESCRIPTION πŸ’–

okay bestie, listen. nng_http_client_connect() starts the process of establishing an HTTP connection. from who to who? from your client to the server β€” and which server, you ask? whichever one was indicated in the URL that client was configured with. no cap, it already knows 🫢

the result of the operation gets stored in the aio when the operation is complete, and you get at it via nng_aio_result(). that's the whole flow, it's giving asynchronous icon behavior πŸ‘‘

and on success? a pointer to the underlying HTTP client (type nng_http_conn *) gets stored in the first output result of the aio, and you can grab it with nng_aio_get_output() using an index of zero (0). zero. not one. don't do that to yourself πŸ™ˆ


πŸ¦‹ RETURN VALUES πŸ¦‹

None. literally nothing comes back from this function directly β€” the tea is all in the aio, iykyk πŸ’…


πŸ”₯ ERRORS πŸ”₯

things can go wrong, and here's the full receipt:

  • NNG_EADDRINVAL β€” the client is configured with an invalid address. wrong address bestie 😭
  • NNG_ECANCELED β€” the operation was aborted. someone hit the cancel, oop.
  • NNG_ECONNREFUSED β€” the TCP connection was refused by the server. they left you on read πŸ’€
  • NNG_ECONNRESET β€” the TCP connection was reset by the server. rude, honestly.
  • NNG_ENOMEM β€” insufficient free memory exists. the laptop said no more 🌈

⭐ EXAMPLE ⭐

here's the whole glow-up in code, fr fr:

nng_aio *aio;
nng_url *url;
nng_http_client *client;
nng_http_conn *conn;
int rv;

// Error checks elided for clarity.
nng_url_parse(&url, "http://www.google.com");
nng_aio_alloc(&aio, NULL, NULL);
nng_http_client_alloc(&client, url);

nng_http_client_connect(client, aio);

// Wait for connection to establish (or attempt to fail).
nng_aio_wait(aio);

if ((rv = nng_aio_result(aio)) != 0) {
        printf("Connection failed: %s\n", nng_strerror(rv));
} else {
        // Connection established, get it.
        conn = nng_aio_get_output(aio, 0);

        // ... do something with it here

        // Close the connection when done to avoid leaking it.
        nng_http_conn_close(conn);
}
ok real talk, notice how the comments literally say "Error checks elided for clarity." β€” so like, don't ship that as-is, bestie. and please, please nng_http_conn_close(conn) when you're done, because leaking connections is not the main character energy you think it is πŸ’€πŸ«Ά

πŸ‘‘ SEE ALSO πŸ‘‘

go read these next, they're all connected (pun absolutely intended):

nng_aio_get_output(3), nng_aio_result(3), nng_aio_wait(3), nng_strerror(3), nng_http_client_alloc(3http), nng_http_conn_close(3http), nng_http_conn_read(3http), nng_http_conn_write(3http), nng(7)


and that's the page, bestie πŸŽ€ this one's from 2026-01-13 and it slays in the NNG reference manual β€” go forth and connect responsibly βœ¨πŸ’–

πŸ“¦ where this page comes from