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

README.md (1570B)


      1 # naett /nɛt:/
      2 
      3 Tiny HTTP client library in C.
      4 
      5 Wraps native HTTP client functionality on macOS, Windows, Linux, iOS and Android in a single, simple non-blocking API.
      6 
      7 ## Using `naett`
      8 
      9 Get the `naett.c` and `naett.h` files and throw them into your project. Check out the [example](./example) for a basic `Makefile` - based setup.
     10 
     11 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()`.
     12 On the other platforms, call with `NULL`.
     13 
     14 See `naett.h` for reference docs.
     15 
     16 ## Platform implementations
     17 
     18 `naett` uses the following HTTP client libraries on each platform:
     19 
     20 | Platform | Library / component | Build with |
     21 | --- | --- | --- |
     22 | macOS, iOS | NSURLRequest | -framework Foundation |
     23 | Windows | WinHTTP Sessions | -lwinhttp |
     24 | Android | java.net.URL | NDK |
     25 | Linux | libcurl | -lcurl -lpthread |
     26 
     27 ### Example
     28 
     29 ```C
     30 #include "naett.h"
     31 #include <unistd.h>
     32 #include <stdio.h>
     33 
     34 int main(int argc, char** argv) {
     35     naettInit(NULL);
     36 
     37     naettReq* req =
     38         naettRequest("https://foo.site.net", naettMethod("GET"), naettHeader("accept", "application/json"));
     39 
     40     naettRes* res = naettMake(req);
     41 
     42     while (!naettComplete(res)) {
     43         usleep(100 * 1000);
     44     }
     45 
     46     if (naettGetStatus(res) < 0) {
     47         printf("Request failed\n");
     48         return 1;
     49     }
     50 
     51     int bodyLength = 0;
     52     const char* body = naettGetBody(res, &bodyLength);
     53 
     54     printf("Got %d bytes of type '%s':\n", bodyLength, naettGetHeader(res, "Content-Type"));
     55     printf("%.100s\n...\n", body);
     56 }
     57 ```