Created
February 23, 2016 22:27
-
-
Save developmentalmadness/9c81ca8a12b810d12de3 to your computer and use it in GitHub Desktop.
Make an http GET request with Akka Http
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
name := """http-client""" | |
version := "1.0" | |
scalaVersion := "2.11.7" | |
libraryDependencies ++= Seq( | |
"com.typesafe.akka" %% "akka-http-experimental" % "2.4.2" | |
) |
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
package com.dvMENTALmadness | |
import akka.actor.ActorSystem | |
import akka.stream.ActorMaterializer | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.model._ | |
import scala.concurrent.Await | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration._ | |
object HttpClient { | |
import akka.http.scaladsl.unmarshalling.Unmarshal | |
implicit val system = ActorSystem("http-client") | |
implicit val materializer = ActorMaterializer() | |
def main(args: Array[String]) : Unit = { | |
var html = Await.result(get("http://akka.io"), 10.seconds) | |
println(html) | |
println("Shutting down...") | |
Http().shutdownAllConnectionPools().foreach(_ => system.terminate) | |
} | |
def get(uri: String) = { | |
val request = HttpRequest(HttpMethods.GET, uri) | |
for { | |
response <- Http().singleRequest(request) | |
content <- Unmarshal(response.entity).to[String] | |
} yield content | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ATM, akka-http-core is no longer "experimental", however akka-http still requires the "experimental" designation