How to fetch POST data in an endpoint handler

#1

I am trying to get an ESP32 to serve a webpage with a form and allow the user to complete the form and submit the data as a POST request. I then want to use that POSTed data to update config settings.

I am able to write a handler and have it handle a POST request, but I can’t figure out how to extract the submitted data in the handler. As near as I can see, the data is passed to the handler as a void *. I have tried casting it to a char * and printing it, but I got a core dump.

I have tested this both with an HTML form and using Postman.

My exact question is, how do I extract data from the void * ??

Here is the handler code:

static void test_handler(struct mg_connection *c, int ev, void *p, void *user_data)
{
  (void) p;

  if (ev != MG_EV_HTTP_REQUEST)
    return;

  LOG(LL_INFO, ("test requested"));

  // Send some data back to caller
  mg_send_response_line(c, 200, "Content-Type: text/html\r\n");
  mg_printf(c, "%s\r\n", "Test endpoint called");

  // Try and print received data to console
  char * d = (char *) user_data;
  if(!d)
  {
    printf("User data is not null");

    // This line causes a core dump
    printf("User Data: %s\r\n", d);
  }
  else
    printf("user_data is null\r\n");

  c->flags |= (MG_F_SEND_AND_CLOSE);

  (void) user_data;
}
#2

The POST data is in the body part of the struct http_message if method is POST.

struct http_message *hm = (struct http_message *) p;

Use mg_get_http_var to extract the variables you need.

#3

Got it working thanks!!!