Akka-Http and easy start with REST APIs

Reading Time: 2 minutes

What is Akka-Http?


Akka-Http is a library for providing HTTP based services. We use it for building REST APIs(Low level or High level REST APIs). It is build on the top of Akka- actors and Streams.
Contains following modules:

  • Akka-http-core
  • Akka-http
  • Akka-Http-testkit
  • akka-http-spray-jason
  • akka-http-xml

Common Abstractions used in Akka-HTTP

  • Http-Request : Used to send a request to the server and it consist of a method(GET, POST etc), a URI, seq of headers
  • Http-Response : Response is whatever server replies to its client. Moreover it consist of status code, Seq of headers, Entity, Protocol
  • Http-Entity : Contains the data bytes of a message with content type and length. However Different type of entities are strict, default, chunked, closed-delimited, Indefinite-length.
  • Marshalling And Unmarshalling : We use marshalling to convert high level object into wire format . Some predefined Marshallers are Array[Byte], String, ByteString, Array[Char], Form Data.
    Whereas Unmarshalling is for converting lower level objects into higher level objects. And some pre-defined un-marshallers are Byte, Boolean, Short, Int, Long, Float, Double, String, Array[String].
  • Http-Headers

Now, we will look for the parts for HIGH LEVEL APIs

1.Directives
Directives are Blocks used for creating a route structure. And it extract the request from RequestContext to make it available for inner route.
For Instance :

2.Path Matcher
When request enters the structure , it has an unmatched path, And the “path matcher” decides on which route it is supposed to hit.

3.Case Class Extractor
Case class Extractor directly map request to case class.

Moreover the work of Parameter directive is to extract three Int values , which will be used to create instance of Colour case class.

4.Routing DSL
It provides and alternative to Low Level Server APIs for expressing your service behaviour as a structure of composable elements (called Directives) in a concise and readable way.

The core of the Routing DSL becomes available with a single import:

Conclusion

In this blog we learned about the structure of Akka-HTTP and also the requirements for building High Level REST APIs using It.

knoldus


Leave a Reply