(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
[inherit('phttpdir:common','phttpdir:phttp','pjsondir:pjson')]
program test(input, output);

var
   con : http;
   request1, response1, response2 : pstr;
   resp, msgs, msg, xtext : cJSON_ptr;
   i : integer;

begin
   request1 := '{"text": "' + 'Hi from Pascal!' + '"}';
   con := http_post('localhost', 8001, '/msgs', 'application/json', 'application/json', fix(request1));
   http_recv_all(con, response1.body, response1.length);
   con := http_get('localhost', 8001, '/msgs', 'application/json');
   http_recv_all(con, response2.body, response2.length);
   resp := pJSON_Parse(response2);
   msgs := pJSON_GetObjectItem(resp, 'message');
   for i := 1 to pJSON_GetArraySize(msgs) do begin
      msg := pJSON_GetArrayItem(msgs, i - 1);
      xtext := pJSON_GetObjectItem(msg, 'text');
      writeln(pJSON_StringValue(xtext));
   end;
   pJSON_Delete(resp);
end.
