Fork me on GitHub

Hacking Scala

#scala #hacking

June 21, 2015 at 5:17pm

Generic OPTIONS HTTP Method Handling With akka-http

The OPTIONS method represents a request for information about the communication options available on the request/response chain identified by the Request-URI.

— HTTP/1.1 Spec

Sounds like pretty helpful HTTP method to use and implement, especially for the REST API. Unfortunately, not many people decide implement it for their own API. I think partly we should thank for this our web frameworks and libraries. In past I tried to implement generic OPTIONS method handler in many different frameworks (like Play, Unfiltered or some java frameworks), but unfortunately my experience was very frustrating to say the least (I really don’t want to write this logic manually for every single resource in my API). In most cases you ether required to use reflection or speculatively try all methods against routing black box in order to find out which HTTP methods are handled and which are not.

akka-http is the first HTTP library in my experience that provides right set of abstractions for this problem. I was able to implement Options handler with it in very natural way and in just few lines of code, so without further ado let me show you how it works.

Keep reading