Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active June 25, 2023 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/1ee46507728c8c3cda4acce710d415cf to your computer and use it in GitHub Desktop.
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/a1a17e69f12b1cf014d9798b2a5d0682accf95ae
// 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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// 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.8"
//> 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