File upload using akka-http in Scala

Lets begin with some introduction of Akka-http first.

Akka HTTP is a layer to expose Actors to the web via HTTP and to enable them to consume HTTP services as a client. It is not an HTTP framework, it is an Actor-based toolkit for interacting with web services and clients. This toolkit is structured into several layers:

  • akka-http-core: A complete implementation of the HTTP standard, both as client and server.
  • akka-http: A convenient and powerful routing DSL for expressing web services.
  • akka-http-testkit: A test harness and set of utilities for verifying your web service implementations.

For more information, see here.

Now its time to go into some code.

To use akka-http, we need to add library in our application.  So to do the same, add following dependencies in build.sbt file.:

Now we will write a route to which we can send our file upload request :

Here, what we have done is, as route will get file upload request, it will take the file data as multipart and will create a file at our local machine in system’s temp folder.

That’s it. Hope you enjoyed. Happy Blogging. !!!

Written by 

Rishi is a Lead Consultant, with experience of more than 7 years. Rishi is product focused developer who loves developing both front-end user interfaces and scalable back-end infrastructure. He is a good team player, quick learner and a humble person. He has good time management skills, aimed to give best results and fully dedicated towards his work & responsibilities. He is able to work as individual and as well as in team. He loves to share his knowledge, therefore he often writes technical blogs.

13 thoughts on “File upload using akka-http in Scala

  1. Hi, I am getting an error in the function processFile() – not enough arguments for method runFold: (implicit materializer: akka.stream.Materializer)scala.concurrent.Future[Array[Byte]]. Unspecified value parameter materializer. I tried adding these:

    import akka.actor.ActorSystem
    import akka.stream.ActorMaterializer
    implicit val system = ActorSystem(“Sys”)
    implicit val materializer = ActorMaterializer()

    Then I started getting error in the uploadFile function in line : processFile(filePath, fileData).map { fileSize =>

    ◾Cannot find an implicit ExecutionContext. You might pass an (implicit ec: ExecutionContext) parameter to your method or import scala.concurrent.ExecutionContext.Implicits.global

    Can you pls help me resolve this, so that the async nature is not lost

    1. Hi Anand. Thanks for pointing me the error.

      I have updated the blog. I missed to include some implicit variables.
      Now please give it a try one more time and let me know if you get any error.

    1. Sorry for late reply. I was away for some reason.
      Actually initially i tried file upload with version 1.0 and fileUpload directive was not there in this version. In later version, this directive has included.

      And, thanks for the information. This fileUpload directive is really a good option.

Leave a Reply

%d bloggers like this: