Skip to content

Instantly share code, notes, and snippets.

@developmentalmadness
Created February 23, 2016 22:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developmentalmadness/9c81ca8a12b810d12de3 to your computer and use it in GitHub Desktop.
Save developmentalmadness/9c81ca8a12b810d12de3 to your computer and use it in GitHub Desktop.
Make an http GET request with Akka Http
name := """http-client"""
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-http-experimental" % "2.4.2"
)
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
}
}
@developmentalmadness
Copy link
Author

ATM, akka-http-core is no longer "experimental", however akka-http still requires the "experimental" designation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment