Skip to content

Instantly share code, notes, and snippets.

@johanandren
Created December 3, 2015 07:39
Show Gist options
  • Save johanandren/44fe6721944705982e9f to your computer and use it in GitHub Desktop.
Save johanandren/44fe6721944705982e9f to your computer and use it in GitHub Desktop.
Minimal example showing request api and unmarshalling response body to a string
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling._
import akka.stream.ActorMaterializer
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
object SimpleClient {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
import system.dispatcher
val http = Http()
val response: Future[HttpResponse] =
http.singleRequest(HttpRequest(HttpMethods.GET, Uri("https://google.com")))
val result = response.map {
case HttpResponse(StatusCodes.OK, headers, entity, _) =>
Unmarshal(entity).to[String]
case x => s"Unexpected status code ${x.status}"
}
println(Await.result(result, 10.seconds))
http.shutdownAllConnectionPools()
system.shutdown()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment