Last active
February 3, 2026 20:22
-
-
Save dacr/1ee46507728c8c3cda4acce710d415cf to your computer and use it in GitHub Desktop.
Fully asynchronous http client call using akka-http will work in all cases, even with chunked responses ! / published by https://github.com/dacr/code-examples-manager #287b4108-ff5a-4abf-af9c-ec3d1422a883/3e844d0801371eef1ed3f8f3e7b75eea9cd75615
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : Fully asynchronous http client call using akka-http will work in all cases, even with chunked responses ! | |
| // keywords : scala, actors, akka, http-client, client | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 287b4108-ff5a-4abf-af9c-ec3d1422a883 | |
| // created-on : 2018-06-19T19:10:04Z | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "2.13.14" | |
| //> using dep "com.typesafe.akka::akka-http:10.2.7" | |
| //> using dep "com.typesafe.akka::akka-stream:2.6.18" | |
| // --------------------- | |
| import akka.http.scaladsl._ | |
| import akka.http.scaladsl.model._ | |
| import akka.http.scaladsl.unmarshalling.Unmarshal | |
| import scala.concurrent._ | |
| import scala.concurrent.duration._ | |
| object TestThat { | |
| implicit val system = akka.actor.ActorSystem("MySystem") | |
| implicit val executionContext = system.dispatcher | |
| val uri = s"http://www.chezmoicamarche.fr" | |
| val futureResponse = Http().singleRequest(HttpRequest(uri = uri)) | |
| val futureResult = futureResponse.flatMap { response => Unmarshal(response.entity).to[String] } | |
| val lastFuture = futureResult.map(println).andThen(_ => system.terminate()) | |
| // Do not exit before the future has completed ;) | |
| def andWait():Unit = Await.ready(lastFuture, 5.seconds) | |
| } | |
| TestThat.andWait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment