Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
the description for this gist
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.model.{ HttpResponse, StatusCodes }
import akka.http.scaladsl.server.Directives.{ complete, options, respondWithHeaders, _ }
import akka.http.scaladsl.server.{ Directive0, Route }
trait CORSHandler {
private val corsResponseHeaders = List(
`Access-Control-Allow-Headers`("Authorization", "Content-Type", "X-Requested-With"))
private def addAccessControlHeaders: Directive0 = {
private def preflightRequestHandler: Route = options {
withHeaders(`Access-Control-Allow-Methods`(OPTIONS, POST, PUT, GET, DELETE)))
def corsHandler(r: Route): Route = addAccessControlHeaders {
preflightRequestHandler ~ r
def addCORSHeaders(response: HttpResponse): HttpResponse =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.