naett.c

Tiny cross-platform HTTP / HTTPS client library in C.
git clone git://git.finwo.net/lib/naett.c
Log | Files | Refs | README | LICENSE

commit bf14e28b68f3999b0f583d1dff3f29f1f651f7ee
parent c36fd5b0500e9bbb15b80c2047a96db8376c1bb3
Author: Erik Agsjö <erik.agsjo@gmail.com>
Date:   Mon,  6 Dec 2021 22:59:27 +0100

Less hard-coded example

Diffstat:
Mexample/main.c | 18++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/example/main.c b/example/main.c @@ -3,16 +3,30 @@ #include <stdio.h> int main(int argc, char** argv) { + if (argc < 2) { + printf("Expected URL argument\n"); + return 1; + } + const char* URL = argv[1]; + naettInit(NULL); - naettReq* req = naettRequest("https://www.dn.se", naettMethod("GET"), naettHeader("accept", "application/json")); + naettReq* req = naettRequest(URL, naettMethod("GET"), naettHeader("accept", "*/*")); naettRes* res = naettMake(req); + while (!naettComplete(res)) { usleep(100 * 1000); } + int status = naettGetStatus(res); + + if (status < 0) { + printf("Request failed: %d\n", status); + return 1; + } + int bodyLength = 0; const char* body = naettGetBody(res, &bodyLength); - printf("Got a %d, %d bytes of type '%s':\n", naettGetStatus(res), bodyLength, naettGetHeader(res, "Content-Type")); + printf("Got a %d, %d bytes of type '%s':\n\n", naettGetStatus(res), bodyLength, naettGetHeader(res, "Content-Type")); printf("%.100s\n...\n", body); }