https://kotlinlang.org logo
#http4k
Title
# http4k
m

Michael Hayes

04/14/2022, 3:27 PM
hi I’ve created a PolyHandler with a http and WS (see https://www.http4k.org/guide/howto/serve_websockets/) When testing I’m getting the “has been blocked by CORS policy” error. I’ve found the code normally used to set the CORS policy but I’m struggling to apply it to the PolyHandler, can you help?
Copy code
CorsPolicy(OriginPolicy.AllowAll(), listOf("content-type", "Authorization"), Method.values().toList(), true)
d

dave

04/14/2022, 5:05 PM
Is it a problem with the webaocket or the http?
m

Michael Hayes

04/15/2022, 8:52 AM
hi This was a problem with the http. The following code is now working ok:
Copy code
val unsafeGlobalPermissive =
    CorsPolicy(OriginPolicy.AllowAll(), listOf("content-type", "Authorization"), Method.values().toList(), true)

val events = defaultEvents()
val apiWithCors = ServerFilters.Cors(unsafeGlobalPermissive)
    .then(CatchAll(events))
    .then(api)

val poly = PolyHandler(apiWithCors, ws)
poly.asServer(Undertow(port)).start()
Issue resolved.
thanks
d

dave

04/15/2022, 8:53 AM
great that you've worked it out.
I was just having a go and was trying to recreate
m

Michael Hayes

04/15/2022, 8:54 AM
thanks for looking into it 👍
d

dave

04/15/2022, 8:55 AM
there's also a trick you can do with lists of filters:
Copy code
val fil1 = Filter { next ->
        {
            next(it)
        }
    }
    val fil2 = Filter { next ->
        {
            next(it)
        }
    }

    val all: Filter = listOf(fil1, fil2).reduce(Filter::then)
💪 1
you can reduce them! 🙂
m

Michael Hayes

04/15/2022, 8:55 AM
👍
4 Views