Copyright © https://mongoose-os.com

Mongoose OS Forum

frame
ATTENTION! This forum has moved to:

https://community.mongoose-os.com

Do not post any new messages.

How do i "post" to an API on a server via https with mongoose OS?

mikaljanmikaljan Taipei, Taiwan

I'm developing a IoT device firmware on the ESP32, I got some instructions from the API service provider as following:

=====================================================
Please use the following API:

POST https://api.backendless.com/AAAAAAAA-BBBB-CCCC-DDDD-FFFFFFFFFFF/AAAAAAAA-AAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE/services/IOTService/registerDevice

HTTP Header:
Content-Type : application/json

Request body:
{
"deviceID": "string",
"deviceHash": "string"
}

Here's a sample request using cURL:
curl -X "POST" "https://api.backendless.com/AAAAAAAA-BBBB-CCCC-DDDD-FFFFFFFFFFF/AAAAAAAA-AAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE/services/IOTService/registerDevice" -H 'Content-Type: application/json' -H 'Accept: application/json' -d $'{ "deviceID": "XXXXX", "deviceHash": "ZZZZZ" }'

=====================================================

How do I implement this on the mongoose OS? Is there an example I can see?
Thanks!

-Michael

Comments

  • mikaljanmikaljan Taipei, Taiwan
    what about the "https" part? the SSL certificate is downloaded upon the connection right?
  • Yes, I think so.

  • Simple example using https://reqres.in/


    struct request { const char *api; const char *payload; }; static const char *s_base_uri = "https://reqres.in/api/"; static struct mg_connection *s_http_connection = NULL; static struct request requests[] = { /* fail */ {"register", "{\"email\": \"sydney@fife\"}"}, /* success */ {"register", "{\"email\": \"sydney@fife\", \"password\": \"123\"}"}, /* fail */ {"login", "{\"email\": \"sydney@fife\"}"}, /* success */ {"login", "{\"email\": \"sydney@fife\", \"password\": \"123\"}"}, }; static const size_t requests_count = sizeof(requests) / sizeof(requests[0]); static void ev_handler(struct mg_connection *c, int ev, void *ev_data, void *user_data) { (void) user_data; if (MG_EV_HTTP_REPLY != ev) { return; } struct http_message *hm = (struct http_message *) ev_data; LOG(LL_INFO, ("reply body: %.*s", (int) hm->body.len, hm->body.p)); c->flags |= MG_F_CLOSE_IMMEDIATELY; } static void timer_cb(void *arg) { (void) arg; static size_t index = 0; const char *extra_headers = "Content-Type: application/json\r\n" "Accept: application/json\r\n"; char uri_buf[64]; char *uri = uri_buf; mg_asprintf(&uri, sizeof(uri_buf), "%s%s", s_base_uri, requests[index].api); LOG(LL_INFO, ("uri=%s, payload=%s", uri, requests[index].payload)); s_http_connection = mg_connect_http(mgos_get_mgr(), ev_handler, NULL, uri, extra_headers, requests[index].payload); if (uri != uri_buf) { free(uri); } ++index; if (index >= requests_count) { index = 0; } }

    Output:

    [Feb 19 20:55:36.155] main.c:70               uri=https://reqres.in/api/login, payload={"email": "sydney@fife"}
    [Feb 19 20:55:36.197] mg_ssl_if_mbedtls.c:35  0x3ffbabb8 ciphersuite: TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
    [Feb 19 20:55:36.212] SW ECDSA verify curve 3 hash_len 32 sig_len 72
    [Feb 19 20:55:36.940] SW ECDH curve 3
    [Feb 19 20:55:38.107] main.c:55               reply body: {"error":"Missing password"}
    [Feb 19 20:55:51.155] main.c:70               uri=https://reqres.in/api/login, payload={"email": "sydney@fife", "password": "123"}
    [Feb 19 20:55:51.212] mg_ssl_if_mbedtls.c:35  0x3ffbabb8 ciphersuite: TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
    [Feb 19 20:55:51.227] SW ECDSA verify curve 3 hash_len 32 sig_len 72
    [Feb 19 20:55:51.946] SW ECDH curve 3
    [Feb 19 20:55:52.733] mgos_mongoose.c:66      New heap free LWM: 209700
    [Feb 19 20:55:53.141] main.c:55               reply body: {"token":"QpwL5tke4Pnpja7X"}
    [Feb 19 20:56:06.156] main.c:70               uri=https://reqres.in/api/register, payload={"email": "sydney@fife"}
    [Feb 19 20:56:06.196] mg_ssl_if_mbedtls.c:35  0x3ffbabb8 ciphersuite: TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
    [Feb 19 20:56:06.212] SW ECDSA verify curve 3 hash_len 32 sig_len 70
    [Feb 19 20:56:06.976] SW ECDH curve 3
    [Feb 19 20:56:07.720] mgos_mongoose.c:66      New heap free LWM: 209636
    [Feb 19 20:56:08.108] main.c:55               reply body: {"error":"Missing password"}
    [Feb 19 20:56:21.156] main.c:70               uri=https://reqres.in/api/register, payload={"email": "sydney@fife", "password": "123"}
    [Feb 19 20:56:21.204] mg_ssl_if_mbedtls.c:35  0x3ffbabb8 ciphersuite: TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
    [Feb 19 20:56:21.219] SW ECDSA verify curve 3 hash_len 32 sig_len 71
    [Feb 19 20:56:21.979] SW ECDH curve 3
    [Feb 19 20:56:22.713] mgos_mongoose.c:66      New heap free LWM: 209200
    [Feb 19 20:56:23.133] main.c:55               reply body: {"token":"QpwL5tke4Pnpja7X"}
    [Feb 19 20:56:36.156] main.c:70               uri=https://reqres.in/api/login, payload={"email": "sydney@fife"}
    [Feb 19 20:56:36.205] mg_ssl_if_mbedtls.c:35  0x3ffbabb8 ciphersuite: TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
    [Feb 19 20:56:36.221] SW ECDSA verify curve 3 hash_len 32 sig_len 70
    [Feb 19 20:56:36.965] SW ECDH curve 3
    [Feb 19 20:56:38.145] main.c:55               reply body: {"error":"Missing password"}
    [Feb 19 20:56:51.156] main.c:70               uri=https://reqres.in/api/login, payload={"email": "sydney@fife", "password": "123"}
    [Feb 19 20:56:51.207] mg_ssl_if_mbedtls.c:35  0x3ffbd82c ciphersuite: TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
    [Feb 19 20:56:51.223] SW ECDSA verify curve 3 hash_len 32 sig_len 71
    [Feb 19 20:56:51.987] SW ECDH curve 3
    [Feb 19 20:56:52.743] mgos_mongoose.c:66      New heap free LWM: 209160
    [Feb 19 20:56:52.963] main.c:55               reply body: {"token":"QpwL5tke4Pnpja7X"}
    [Feb 19 20:57:06.157] main.c:70               uri=https://reqres.in/api/register, payload={"email": "sydney@fife"}
    [Feb 19 20:57:06.202] mg_ssl_if_mbedtls.c:35  0x3ffbb1f8 ciphersuite: TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
    [Feb 19 20:57:06.217] SW ECDSA verify curve 3 hash_len 32 sig_len 72
    [Feb 19 20:57:06.951] SW ECDH curve 3
    [Feb 19 20:57:08.110] main.c:55               reply body: {"error":"Missing password"}
    [Feb 19 20:57:21.157] main.c:70               uri=https://reqres.in/api/register, payload={"email": "sydney@fife", "password": "123"}
    [Feb 19 20:57:21.212] mg_ssl_if_mbedtls.c:35  0x3ffbb2dc ciphersuite: TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
    [Feb 19 20:57:21.227] SW ECDSA verify curve 3 hash_len 32 sig_len 71
    [Feb 19 20:57:21.942] SW ECDH curve 3
    [Feb 19 20:57:22.907] main.c:55               reply body: {"token":"QpwL5tke4Pnpja7X"}
    
Sign In or Register to comment.