Adding an Event handler for "HTTP GET" to API using mg_connect_http?

Hello everyone,
Having an issue with sending data to an API on the web.

  1. My goal is:
    To send a HTTP GET to an API, only need the send an URL.

  2. My actions are:
    I have been looking at the post:
    I am not great at C and I am having trouble understanding how to use/create the event handler.

This is the code I have added:

struct mg_connection *mg_connect_http(

    struct mg_mgr *mgr,

    MG_CB(mg_event_handler_t event_handler, void *user_data), apiUrl, NULL, NULL);

    mg_connect_http(mgos_get_mgr(), event_handler, apiUrl, NULL, NULL);
  1. The result I see is:
    My code wont build.

  2. My expectation & question is:
    A full example of creating the event handler and using it to do a HTTP GET would be awesome.

Thanks in advance,

Simple application to get leap-seconds.list from

1 Like

Awesome! It works! Thanks!

Now the URL is “called” at boot.
I don’'t really understand the code, how do I “call” the URL again? Do I need to execute:
mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, net_event_handler, NULL);
Every time I want to send something to the API (the URL)?

I read the sensor, I then want to call an the API URL ++ “field0=newValue”. If that makes sense.

Kind regards and thanks again,

This is what you need.

Ah, that is the call to the URL?
I will test that! Thanks!

I followed the example and I can get it working to both “” and “” with “GET” response.

However, when I trying to connect local simple test server (“”), then I get error

  • MG_EV_CONNECT : -13

The call I use is

  • char s_url[100]=“”
  • char s_url[100]=“
  • struct mg_connection *conn = mg_connect_http(mgr, ev_handler, NULL, s_url, NULL,NULL);
    Both s_url fails

I have validated the test server using CURL which is ok

  • curl --request GET

Modify the line

LOG(LL_INFO, ("%s:%d - err=%d", __FUNCTION__, __LINE__, *(int *) evd));

to get an error string

        LOG(LL_INFO, ("%s:%d - MG_EV_CONNECT error: %d (%s)", __FUNCTION__,
                      __LINE__, err, strerror(err)));

I get it working. A mistake in LOG print of the result.

However when I trying some real site link. e.g.
The first time is “OK”, it receives the MG_EV_HTTP_CHUNK with data.
But when I try few times, there is some memory leaks somewhere.

I’ve noticed that some sites need an extra header Connection: close:

mg_connect_http(mgr, ev_handler, NULL, server, "Connection: close\r\n", NULL);