Good to hear it's working for you now.
Here is the example, in case you (or sombody who finds this thread later) is interested in a reduced C example (smaller as compared to the embedded_c example).
It is built on Linux using the command:
gcc examples/demo.c src/civetweb.c -Iinclude -DNO_SSL -DNO_FILES -lpthread -odemo
run using
./demo
and tested with curl using
--> Got 2078 bytes of POST body data (expected 2078 bytes)
And this is the source (demo.c):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h> // or #include <windows.h> for Windows
#include "civetweb.h"
int with_https = 0;
int post_handler(struct mg_connection *conn, void *cbdata)
{
const struct mg_request_info *request_info = mg_get_request_info(conn);
if ((request_info == NULL) || (request_info->request_method == NULL)) {
mg_send_http_error(conn, 500, "Internal Error"); // will not happen
return 500;
}
if (0!=strcmp(request_info->request_method, "POST")) {
mg_send_http_error(conn, 405, "Only POST allowed");
return 405;
}
long long expected_len = request_info->content_length;
long long read_len = 0;
while (1) {
char buffer[1024];
int rnow = mg_read(conn, buffer, sizeof(buffer));
if (rnow <= 0) {
/* No more data. Connection closed --> rnow == 0, Connection error --> rnow < 0 */
break;
} else {
/* Append rnow bytes from buffer to .. wherever you want to put the post data */
read_len += rnow;
}
}
char response[1024];
int len = sprintf(response, "Got %lli bytes of POST body data (expected %lli bytes)\n", read_len, expected_len);
mg_send_http_ok(conn, "text/plain", len);
mg_write(conn, response, len);
return 200;
}
static void init_context(const struct mg_context *ctx)
{
mg_set_request_handler(ctx, "/post-me", post_handler, NULL);
}
static int log_message(const struct mg_connection *conn, const char *message)
{
fprintf(stderr, "Error: %s\n", message);
return 0;
}
volatile int running;
int main(int argc, char *argv[])
{
/* Init CivetWeb library once for every process */
mg_init_library(0 /* HTTP only: 0, HTTP+HTTPS: MG_FEATURES_TLS */);
struct mg_callbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.init_context = init_context;
callbacks.log_message = log_message;
const char *configuration_options[] = {"listening_ports", "8081", NULL, NULL};
struct mg_context * ctx = mg_start(&callbacks, NULL, &configuration_options);
if (!ctx) {
/* Error */
mg_exit_library();
return 1;
}
running = 1;
while (running) {
sleep(1); // or Sleep(1000) for Windows
}
mg_stop(ctx);
mg_exit_library();
return 0;
}