/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "vms_http.h"
#include "cJSON.h"

int main()
{
    char request1[1000];
    sprintf(request1, "{\"text\": \"%s\"}", "Hi from C!");
    struct vms_http *ctx1 = http_post("localhost", 8001, "/msgs", "application/json", "application/json", request1);
    char response1[10000];
    short resp1len;
    http_recv_all(ctx1, response1, sizeof(response1), &resp1len);
    response1[resp1len] = 0;
    http_close(ctx1);
    struct vms_http *ctx2 = http_get("localhost", 8001, "/msgs", "application/json");
    char response2[10000];
    short resp2len;
    http_recv_all(ctx2, response2, sizeof(response2), &resp2len);
    response2[resp2len] = 0;
    http_close(ctx2);
    cJSON *resp = cJSON_Parse(response2);
    cJSON *msgs = cJSON_GetObjectItem(resp, "message");
    for(int i = 0; i < cJSON_GetArraySize(msgs); i++)
    {
        cJSON *msg = cJSON_GetArrayItem(msgs, i);
        cJSON *text = cJSON_GetObjectItem(msg, "text");
        printf("%s\n", text->valuestring);
    }
    cJSON_Delete(resp);
    return 0;
}
