pistacheio / pistache

A high-performance REST toolkit written in C++
https://pistacheio.github.io/pistache/
Apache License 2.0
3.12k stars 688 forks source link

Swagger UI CORS problem #1156

Open h1laryz opened 11 months ago

h1laryz commented 11 months ago

When adding to swagger documentation any header (in my case x-api-key) getting error Access to fetch at 'http://127.0.0.1:11600/........' from origin 'http://localhost:11500' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Actually, I have this code

void RestApiServer::setupCors()
{
    pistacheRouter_.addMiddleware(
            []( const Pistache::Http::Request& request, Pistache::Http::ResponseWriter& writer )
            {
                writer.headers().add<Pistache::Http::Header::AccessControlAllowMethods>("GET,POST,DELETE,PUT,PATCH,OPTIONS");
                writer.headers().add<Pistache::Http::Header::AccessControlAllowHeaders>("x-api-key");  // tryed this 
                writer.headers().add<XApiKey>(); // tryed this 
                writer.headers().add< Pistache::Http::Header::AccessControlAllowOrigin >( "*" );
                return true;
            });
}

Also, without x-api-key header AccessControlAllowOrigin works fine can anyone help, please?