Sending HTTP Requests in 5 Minutes With Scala and Akka HTTP
Join the DZone community and get the full member experience.
Join For FreeYou can find this article in video form on YouTube or embedded below.
The Tiny Setup
val akkaVersion = "2.5.26"
val akkaHttpVersion = "10.1.11"
libraryDependencies ++= Seq(
// akka streams
"com.typesafe.akka" %% "akka-stream" % akkaVersion,
// akka http
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion,
)
Then, in a Scala application, I'm going to write a piece of small boilerplate because Akka HTTP needs an actor system to run:
xxxxxxxxxx
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
import system.dispatcher
Sending HTTP Requests
application/x-www-form-urlencoded
.
xxxxxxxxxx
val source =
"""
|object SimpleApp {
| val aField = 2
|
| def aMethod(x: Int) = x + 1
|
| def main(args: Array[String]) = {
| println(aMethod(aField))
| }
|}
""".stripMargin
and then let me create an HTTP request for it:
xxxxxxxxxx
val request = HttpRequest(
method = HttpMethods.POST,
uri = "http://markup.su/api/highlighter",
entity = HttpEntity(
ContentTypes.`application/x-www-form-urlencoded`,
s"source=${URLEncoder.encode(source.trim, "UTF-8")}&language=Scala&theme=Sunburst"
)
)
where I've named the arguments in the call for easy reading. In the Akka HTTP, an
HttpRequest
contains the HTTP method (POST in our case). The URI and a payload in the form of an
HttpEntity
. We specify the content type per the description specified in the API — notice the backticks for the name of the field — and the actual string we want to send, as described by the API. In practice, you can send other strings, like JSON — I'll show you how to auto-convert your data types to JSON auto-magically in another article.
xxxxxxxxxx
def simpleRequest() = {
val responseFuture = Http().singleRequest(request)
responseFuture.flatMap(_.entity.toStrict(2 seconds)).map(_.data.utf8String).foreach(println)
}
The Akka HTTP client call is simple: just call the
singleRequest
method. You obtain a Future containing an HTTP response, which we can then unpack. We use its entity (= its payload) and convert it to a strict entity, meaning that we take its whole content in memory. We then take its data, which is a sequence of bytes, and convert that to a string. And we're done.
Hide it All
xxxxxxxxxx
def highlightCode(myCode: String): Future[String] = {
val responseFuture = Http().singleRequest(
HttpRequest(
method = HttpMethods.POST,
uri = "http://markup.su/api/highlighter",
entity = HttpEntity(
ContentTypes.`application/x-www-form-urlencoded`,
s"source=${URLEncoder.encode(myCode.trim, "UTF-8")}&language=Scala&theme=Sunburst"
)
)
)
responseFuture
.flatMap(_.entity.toStrict(2 seconds))
.map(_.data.utf8String)
}
And then you can go on with your day: pass a string, expect a future containing an HTML highlighting. All done!
Published at DZone with permission of Daniel Ciocirlan. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments