Case Insensitive routing with akka-http


(Adarsh J) #1

Is there a global setting which allows the Path matchers to work in a case insensitive way?

I’ve a very specific case where I want to do

pathPrefix("string")

and want it to be matched without being case sensitive. I do know we could have
pathPrefix(Segment) => (segment) then convert segment toLower and match, but the above case the string has to specifically match in a case insensitive way, is there a way to do this?


(Josep Prat) #2

Hi Adarsh,

Akka HTTP tries to adhere to the standards, and according to them, URL paths are case sensitive. There isn’t any configuration parameter where this can be toggled or changed.

Of course, you can always write your own path directive that checks prefixes in a case insensitive way. One way to do so is to map the request’s path to be lowercase and then convert the path prefixes strings also to lowercase. This should do the trick.


(Josep Prat) #3

It could be something like this:

private def caseInsensitivePathPrefix(path: String)(inner: Route) = {
    mapRequest ( request => request.copy(uri = Uri(request.uri.path.toString.toLowerCase))){
        pathPrefix(path.toLowerCase)(inner)
      }
    }

Please note that this is a quick code I hack together on the spot, check it before using it in production.


(Adarsh J) #4

Thanks Josep, sure, I just wanted to know the right direction to look at. thanks for the quick snippet!


(Josep Prat) #5

You’re welcome!