File upload using akka-http in Scala

Table of contents
Reading Time: 2 minutes

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 tech enthusiast with having around 10 years of experience who loves to solve complex problems with pure quality. He is a functional programmer and loves to learn new trending technologies. His leadership skill is well prooven and has delivered multiple distributed applications with high scalability and availability by keeping the Reactive principles in mind. He is well versed with Scala, Akka, Akka HTTP, Akka Streams, Java8, Reactive principles, Microservice architecture, Async programming, functional programming, distributed systems, AWS, docker.

13 thoughts on “File upload using akka-http in Scala2 min read

  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.

  2. Hello Rishi,
    One other thing I noticed is that, when I try to upload a file of size 150 MB, the POST request is failing after around 8-9 seconds. Any idea how to fix it?

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading