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

main.c (843B)


      1 #include "naett.h"
      2 #include <unistd.h>
      3 #include <stdio.h>
      4 
      5 int main(int argc, char** argv) {
      6     if (argc < 2) {
      7         printf("Expected URL argument\n");
      8         return 1;
      9     }
     10     const char* URL = argv[1];
     11 
     12     naettInit(NULL);
     13 
     14     naettReq* req = naettRequest(URL, naettMethod("GET"), naettHeader("accept", "*/*"));
     15     naettRes* res = naettMake(req);
     16 
     17     while (!naettComplete(res)) {
     18         usleep(100 * 1000);
     19     }
     20 
     21     int status = naettGetStatus(res);
     22 
     23     if (status < 0) {
     24         printf("Request failed: %d\n", status);
     25         return 1;
     26     }
     27 
     28     int bodyLength = 0;
     29     const char* body = naettGetBody(res, &bodyLength);
     30     printf("Got a %d, %d bytes of type '%s':\n\n", naettGetStatus(res), bodyLength, naettGetHeader(res, "Content-Type"));
     31     printf("%.100s\n...\n", body);
     32 
     33     naettClose(res);
     34     naettFree(req);
     35 }