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 9af60e5eb9a4584cee8cff320fb79feb227b5b32
parent 4eb9ee2d7ee4553cf600d1123047a3a16ffb0c2b
Author: Erik Agsjö <erik.agsjo@gmail.com>
Date:   Mon,  6 Dec 2021 23:44:19 +0100

README

Diffstat:
AREADME.md | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,57 @@ +# naett /nɛt:/ + +Tiny HTTP client library in C. + +Wraps native HTTP client functionality on macOS, Windows, iOS and Android in a single, simple non-blocking C API. + +## Using `naett` + +Get the `naett.c` and `naett.h` files and throw them into your project. Checkout the [example](./example) for a basic `Makefile` - based setup. + +The library needs to be initialized by a call to `naettInit()`. On Android, you need to provide a `JavaVM*` handle in the call to `naettInit()`. On the other platforms, call with `NULL`. + +See `naett.h` for reference docs. + +## Platform implementations + +`naett` uses the following HTTP client libraries on the different platforms: + +| Platform | Library / component | Build with | +| --- | --- | --- | +| macOS, iOS | NSURLRequest | -framework Foundation | +| Windows | WinHTTP Sessions | -lwinhttp | +| Android | java.net.URL | NDK | +| Linux | libcurl | -lcurl -lpthread | +| | | + +### Example + +```C +#include "naett.h" +#include <unistd.h> +#include <stdio.h> + +int main(int argc, char** argv) { + naettInit(NULL); + + naettReq* req = + naettRequest("https://foo.site.net", naettMethod("GET"), naettHeader("accept", "application/json")); + + naettRes* res = naettMake(req); + + while (!naettComplete(res)) { + usleep(100 * 1000); + } + + if (naettGetStatus(res) < 0) { + printf("Request failed\n"); + return 1; + } + + int bodyLength = 0; + const char* body = naettGetBody(res, &bodyLength); + + printf("Got %d bytes of type '%s':\n", bodyLength, naettGetHeader(res, "Content-Type")); + printf("%.100s\n...\n", body); +} +```