ipkn / crow

Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)
BSD 3-Clause "New" or "Revised" License
7.43k stars 889 forks source link

How to disable OPTIONS request method? #421

Open maheshlode opened 10 months ago

maheshlode commented 10 months ago

I tried using the following code snippet to handle a POST request method in my application, and I want to make sure that it doesn't handle any other request methods. However, when I perform an OPTIONS request, it returns a 204 No Content response status, indicating that it's processing the OPTIONS request. I also attempted to replace POST with OPTIONS, but the code doesn't seem to enter the function. CROW_ROUTE(app, "URL").methods("POST"_method).name("hello")([](const crow::request& req){});

Using CORS

auto &cors = app.get_middleware<crow::CORSHandler>();

// Configure CORS
// clang-format off
cors
  .global()
    .methods("POST"_method, "GET"_method)
  .prefix("/")
    .origin("URL")
    .allow_credentials();
// clang-format on
``
// OPTIONS request handling for "/write"
CROW_ROUTE(app, "/write")
    .methods(crow::HTTPMethod::OPTIONS)
([](const crow::request& req) {
    return crow::response(crow::status::OK);
});

// GET request handling for "/write"
CROW_ROUTE(app, "/write")
    .methods(crow::HTTPMethod::GET)
([](const crow::request& req) {
    CROW_LOG_INFO << "Sending response";
    return crow::response(crow::status::OK, "This is a response");
});`

Also I tried using core middleware, but it didn't work as expected. It is not getting inside the OPTIONS method.