NNG_HTTP_CLIENT_CONNECT(3http)
π
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
- alpine 3.24: nng-doc 1.11-r0 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms