Created
December 3, 2015 07:39
-
-
Save johanandren/44fe6721944705982e9f to your computer and use it in GitHub Desktop.
Minimal example showing request api and unmarshalling response body to a string
This file contains 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
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