0

im currently building a REST-Api with Crow in C++ and I kinda stuck. Everything's works pretty much out of the box as expected but the POST Endpoint. Im unable to solve the CORS problem:

XMLHttpRequest cannot load http://localhost:18080/"ENDPOINT-ROUTE" due to access control checks.

http://localhost:18080/"ENDPOINT-ROUTE"
Failed to load resource: Origin http://localhost:4200 is not allowed by Access-Control-Allow-Origin.

Code:

        CROW_ROUTE(app, "ENDPOINT-ROUTE").methods("POST"_method)([&app ,&m_session](const crow::request& req){
                crow::response response("");
                response.add_header("Access-Control-Allow-Origin", "*");

                return response;
            });

The request from my angular project:


  create(object: Object)
  {
    httpOptions = {
        headers: new HttpHeaders({
          'Content-Type':  'application/json',
          'Access-Control-Allow-Headers':'*'
        })
      };

    return this.http.post<Object>("http://localhost:18080/ENDPOINT-ROUTE"), object, httpOptions);
 }

Thanks for your help!

Tobi K.
  • 1
  • 2

1 Answers1

4

https://github.com/CrowCpp/Crow has a CORS middleware you can use to set your rules. We haven't done the full documentation on it yet, but we do have an example.