mongoose/examples/restful_client/restful_client.c
Marko Mikulicic c82ccd6cf7 Simplify http client example
PUBLISHED_FROM=64a91c5d261338c7c224340943d63d4b753ee303
2016-03-08 08:44:21 +01:00

52 lines
1.2 KiB
C

/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
#include "mongoose.h"
/* RESTful server host and request URI */
static const char *s_url =
"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=cesanta";
static int s_exit_flag = 0;
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
struct http_message *hm = (struct http_message *) ev_data;
int connect_status;
switch (ev) {
case MG_EV_CONNECT:
connect_status = *(int *) ev_data;
if (connect_status != 0) {
printf("Error connecting to %s: %s\n", s_url, strerror(connect_status));
s_exit_flag = 1;
}
break;
case MG_EV_HTTP_REPLY:
printf("Got reply:\n%.*s\n", (int) hm->body.len, hm->body.p);
nc->flags |= MG_F_SEND_AND_CLOSE;
s_exit_flag = 1;
break;
default:
break;
}
}
int main(void) {
struct mg_mgr mgr;
struct mg_connection *nc;
mg_mgr_init(&mgr, NULL);
nc = mg_connect_http(&mgr, ev_handler, s_url, NULL, NULL);
mg_set_protocol_http_websocket(nc);
printf("Starting RESTful client against %s\n", s_url);
while (s_exit_flag == 0) {
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}