Migrating from Spray to Akka HTTP

by Nikita Melkozerov - 3 May 2016

Spray is a well-known HTTP library in the Scala ecosystem. It was released in 2011, and since then it’s been widely used by the Scala community. It was recently announced that Spray would be replaced with Akka HTTP, thus cementing Akka HTTP as the successor of Spray. It’s maintained by Lightbend and it's been recommended that users migrate to it soon.

However, migration from one major version of a library to another is not an easy task. Very often it requires you to spend some time reading the source code in order to figure out how to use certain features, as well as how to migrate existing logic.

This post will demonstrate what changes should be applied in order to migrate your app from Spray to Akka HTTP. The following steps don’t have a particular order, as it depends on which areas need to be rewritten.

Packages

In order to have the latest Akka HTTP packages, all previous Spray dependencies now need to be replaced by the following:

Headers

All HTTP headers have been moved to the akka.http.scaladsl.model.headers._ package.

Form fields and file upload

With the streaming nature of http entity, it’s important to have a strict http entity before accessing multiple form fields or use file upload directives. One solution might be using next directive before working with form fields:

While this list isn’t an exhaustive collection of all the changes you need to do, it covers the trickiest ones that exist. One major drawback of Akka HTTP is that it’s not as mature as Spray, and it’s performance is not optimised yet. Users may also notice a lack of documentation for some cases.

Having said that, it would be a good idea to keep the above issues in mind during this process. Happy migration!