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

#1

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: https://community.mongoose-os.com/t/how-to-call-restful-api-in-esp8266-mongoose-os/77
    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,
Andreas

#2

Simple application to get leap-seconds.list from https://www.ietf.org

1 Like
#3

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,
Andreas

#4

This is what you need.

#5

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

#6

I followed the example and I can get it working to both “https://www.ietf.org” and “https://www.example.com” with “GET” response.

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

  • MG_EV_CONNECT : -13

The call I use is

  • char s_url[100]=“192.168.0.14:80”
    or
  • char s_url[100]=“http://192.168.0.14:80
  • 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 192.168.0.14:80
#7

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)));
#8

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

However when I trying some real site link. e.g. https://mongoose-os.com
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.

#9

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);