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. !!!
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
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.
Hello Rishi,
Is there a specific reason you did not use standard fileUpload akka-http directive? http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.2/scala/http/routing-dsl/directives/file-upload-directives/fileUpload.html#fileupload
Thanks for this information
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.
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?
Hello Anand,
150MB issue may be related to akka-http config. Default incoming request size is 8MB. Have you tried increasing akka.http.server.parsing.max-content-length ?
http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.2/scala/http/configuration.html
Reblogged this on Rishi Khandelwal.
Looks good, How do we get form-data inside multipart?
Hi avis,
Please find below link for your query :
https://blog.knoldus.com/2016/06/01/a-basic-application-to-handle-multipart-form-data-using-akka-http-with-test-cases-in-scala/