Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Middleware does not run on POST request, only on GET request. #960

Open
Rz-Rz opened this issue Dec 4, 2024 · 1 comment
Open
Labels
question Issue can be closed by providing information

Comments

@Rz-Rz
Copy link

Rz-Rz commented Dec 4, 2024

I am using Crow 1.2.

I have created the following middleware :

struct DynamicCORS {
  struct context {};

  void before_handle(crow::request &req, crow::response &res, context &ctx) {
    // Find the Origin header
    auto origin_iter = req.headers.find("Origin");
    if (origin_iter != req.headers.end()) {
      std::cout << "Origin: " << origin_iter->second << std::endl;
    } else {
      std::cout << "Origin: Not found" << std::endl;
    }

    // Print the HTTP method
    std::cout << "Method: " << crow::method_name(req.method) << std::endl;

    // Print all headers
    std::cout << "Headers:" << std::endl;
    for (const auto &header : req.headers) {
      std::cout << "  " << header.first << ": " << header.second << std::endl;
    }
    // Handle preflight (OPTIONS) requests
    if (req.method == crow::HTTPMethod::Options) {
      auto origin_iter = req.headers.find("Origin");
      if (origin_iter != req.headers.end()) {
        std::string origin = origin_iter->second;
        res.add_header("Access-Control-Allow-Origin", origin);
        res.add_header("Access-Control-Allow-Credentials", "true");
        res.add_header("Access-Control-Allow-Methods",
                       "GET, POST, PUT, DELETE, OPTIONS");
        res.add_header("Access-Control-Allow-Headers",
                       "Content-Type, Authorization, X-Requested-With");
        res.code = 204; // No Content
        res.end();
      }
    }
  }

  void after_handle(crow::request &req, crow::response &res, context &ctx) {
    auto origin_iter = req.headers.find("Origin");
    if (origin_iter != req.headers.end()) {
      std::string origin = origin_iter->second;
      res.add_header("Access-Control-Allow-Origin", origin);
      res.add_header("Access-Control-Allow-Credentials", "true");
    }
  }
};

It is designed to reflect the host origin, similar to setting '*' but it works with the browser.
I am setting the app with this custom Middleware like this :

  crow::App<DynamicCORS> app;

For get request I have no problem and I also get the logs from my app.
Here's the response headers :

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://0.0.0.0:8000
Content-Type: application/json
Content-Length: 67
Server: Crow/master
Date: Wed, 04 Dec 2024 11:57:51 GMT
Connection: Keep-Alive

However for POST requests I get this :

HTTP/1.1 204 No Content
Allow: OPTIONS, HEAD, DELETE, GET, POST
Content-Length: 0
Server: Crow/master
Date: Wed, 04 Dec 2024 11:57:54 GMT

And I don't get any logs from my app, so the middleware was actually not triggered. Here's how I register my routes :

  CROW_ROUTE(app, "/example/<string>")
      .methods(crow::HTTPMethod::Post, crow::HTTPMethod::Get)(get_example);
@gittiver
Copy link
Member

gittiver commented Dec 4, 2024

There is no option request before POST?

@gittiver gittiver added the question Issue can be closed by providing information label Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issue can be closed by providing information
Projects
None yet
Development

No branches or pull requests

2 participants