Sending HTTP Requests in 5 Minutes With Scala and Akka HTTP
Join the DZone community and get the full member experience.Join For Free
You can find this article in video form on YouTube or embedded below.
The Tiny Setup
Then, in a Scala application, I'm going to write a piece of small boilerplate because Akka HTTP needs an actor system to run:
Sending HTTP Requests
and then let me create an HTTP request for it:
where I've named the arguments in the call for easy reading. In the Akka HTTP, an
HttpRequestcontains 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.
The Akka HTTP client call is simple: just call the
singleRequestmethod. 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
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.