Skip to content

Handling OPTIONS request with mongoose #1443

Discussion options

You must be logged in to vote

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

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by cpq
Comment options

You must be logged in to vote
1 reply
@cpq
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants