Akka HTTP Routing

In this blog, I try to describe some basic routes of Akka-HTTP, redirection of the route and how to handle extra parameters that come in route.

so before writing the routes, you have to set the environment for it.

implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher

These are the variables that are you need to define provisionally before writing the routes.

so now let’s writing the route –

val route =
      path("hello") {
        get {
          complete("Say hello to akka-http")

now you have to bind this route to a port

val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)

Here we define simplest route “/hello”  and whatever response you want to return you have to give into complete() method.
and we bind this route to the localhost:8080.

so let’s move another type of rote –

If you want to send some segment in the path in that case route will be like –

  val route =
      path("hello"/Segment) { name =>
        get {
          complete("say hello to"+name)

so how can hit this route by using –


so here shubham is the segment which is part of the path.

Handling URL rewriting in Akka-HTTP:-

some time you needs to send some extra fields but not a part of the path. so how we can do this by Akka-HTTP.

 def route : Route =
      path("my"/"route") {
        parameters("id", "password") { (id, password) =>
        get {
          if(id.toInt==1 && password.toInt==1234) complete("Login successFully") else complete("sorry you are not authorized")

we can send the request to this route by –


PathPrefix in Akka-Http:-

so now we talk about another type of route which is path-prefix.There are some use cases where some times you need to send some segment to the route or some, time not into the route.

suppose your route is like –


and another time you need a route which is same as previous but needs to send an extra field like –


so, in this case, you don’t have to write two different routes of the same name . we will create a single route which will work for both using path-prefix like –

   val route =
      pathPrefix("my"/"route")  {

      pathEnd {
        get {
          complete("Say hello to akka-http")
      } ~ path(Segment) { (name) =>
          complete("Say hello to"+name)


you can hit this route by –



Redirection to the different Route: –

we can also redirect the request from one route to the another route –

 def route =
	      path("login") {
		get {
		       val url = "http://localhost:8080/my/route"
                       val response = Http().singleRequest(HttpRequest(uri = url + "?id=1&password=1234"))

Here use the single request method of Http() and redefine it’s uri to the new path. so that’s the way we can redirect from one route to the another route which I have defined above.
Hope this blog will help you to know some basics route. this is not the end I will come back with same advanced routes.

you can see the Whole Example by click Here

References –


This entry was posted in Akka, akka-http, Scala. Bookmark the permalink.

One Response to Akka HTTP Routing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s