Handling OPTIONS request with mongoose #1443
Answered
by
cpq
NRollo
asked this question in
Ask questions, get answers
-
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.
Answered by
cpq
Jan 7, 2022
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.
0 replies
Answer selected by
cpq
-
@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.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah that sounds good. Something like this: