This document provides an overview of Akka HTTP, a library for building HTTP-based services using Scala and Akka. It describes the common abstractions used in Akka HTTP like HTTP requests, responses, entities, and marshalling/unmarshalling. It also explains the low-level and high-level APIs, with the low-level API providing basic request handling functionality and the high-level API using directives and routing DSL for defining routes in a more flexible way.
2. Agenda
â Overview of Akka Http
â Common Abstractions
â Low level API
â High level API
â Demo
3. Overview of Akka-Http
â A library for providing HTTP based services.
â Used for building REST APIs.
â Build on the top of Akka- actors and Streams.
7. Http Request
â Used to send a request to the server
â Akka-Http request consists of -
â method (GET, POST, etc.)
â A URI
â Seq of headers
â Entity (body data), and
â Protocol
HttpRequest (method, URI, List(headers), entity, protocol)
8. Http Response
â Response is what server replies to its client.
â HttpResponse consists of
â Status code
â Seq of headers
â Entity
â Protocol
HttpResponse(StatusCode, List(headers), entity, protocol)
9. Http Entity
â Contains the data bytes of a message with
content-type and length.
â Different kinds of entities:
â Strict
â Default
â Chunked
â ClosedDelimited
â IndefiniteLength
10. Marshalling
â To convert higher level objects into wire-format.
â Some pre-defined marshallers :
â Array[Byte]
â String
â ByteString
â Array[Char]
â FormData
11. Unmarshalling
â To convert lover level objects into higher level
objects.
â Some pre-defined unmarshallers:
â Byte, Boolean
â Short, Int, Long , Float, Double
â String
â Array[String]
12. Low Level API
â The implementation is in akka-http-core
module.
â It provides support for :
â Connection management
â Parsing and rendering of messages and headers
â Timeout management
â Response ordering
13. Low Level API (Cont...)
Http().bindAndHandleSync(reqHandler, interface, portAddr)
This method specifies the interface and port to bind for
handling incoming Http requests.
14. Low Level API (Cont...)
Low Level API is based simply on mapping from request to response
15. High Level API
â Low level API becomes uneasy to handle when we
need to create large number of routes.
â For this we use should Higher Level APIs.
â Provides a flexible ways to create routes.
â These are located in akka-http module.
â Low level API becomes uneasy to handle when we
need to create large number of routes.
â For this we use should Higher Level APIs.
â Provides a flexible ways to create routes.
â These are located in akka-http module.
16. High Level API (Cont..)
â Directives
â Routing DSL (alternative to Low level API)
â Rejection & Exception Handling
â Path Matcher
â Case class extraction
17. Directives
â A block used for creating route structure.
â Extract the requests from RequestContext to make
it available for inner route.
18. Path Matcher
â When request enters the structure, it has an
unmatched path.
â The path matcher decides on which route it is
supposed to hit.
path(âfooâ / âbarâ)
means path is foo / bar
Actors are used when an application is to be distributed across different threads.
And, when it is to be distributed over the network, we should use Akka-http
Akka-http-core â provides support for low level implementation like Connection management
Akka-http â for high level, like routing DSL
Akka-http-testkit â testkit for testing routes
Akka-http-spray-json â for data handling in json
Akka-http-xml â for data handling in xml format
Akka-http has large set of case-classes to work with.
Some are on client side and some on server side.
But there are some common classes that are not specific to client or server.
Response codes are available in StatusCode
Strict â Used for small data which is already available in memory
Default â for data streaming
Chunked â for data handling in chunks for unknown length
ClosedDelimited â close connection after transmitting message
IndefiniteLength â for data streaming for infinite length
We can also create our custom marshal for our own types like case-classes
All marshallers should be available implicitly.
We can also have our custom unmarshallers for our own types like case-classes
Directives extract the requests from RequestContext and make them available to its inner route