Handling OPTIONS request with mongoose #1443
-
| I was wondering, what is the recommended procedure for handling OPTIONS requests (preflight) with the mongoose webserver? I am thinking of a response like: Thanks! | 
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
| Yeah that sounds good. Something like this: static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
  if (ev == MG_EV_HTTP_MSG) {
    struct mg_http_message *hm = (struct mg_http_message *) ev_data;
    if (mg_strcmp(hm->method, mg_str("OPTIONS")) == 0) {
      mg_http_reply(c, 204, "Allow: OPTIONS, GET, POST\r\n", "");
    } else if (mg_http_match_uri(hm, "/api/foo")) {
      mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);  // Serve REST
    } else {
      struct mg_http_serve_opts opts = {.root_dir = "."};
      mg_http_serve_dir(c, ev_data, &opts);
    }
  }
} | 
Beta Was this translation helpful? Give feedback.
-
| @cpq Thanks for the reply - could you elaborate a bit on the last part of the code: Thanks! | 
Beta Was this translation helpful? Give feedback.
Yeah that sounds good. Something like this: