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 –

 http://localhost:8080/hello/shubham

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 –

http://localhost:8080/my/route?id=1&password=1234

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 –

 path("my"/"route")

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

 path("my"/"route"/Segment)

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 –

 http://localhost:8080/my/route  

 http://localhost:8080/my/route/shubham  

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"))
                       complete(response)
		}
	      }

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 –

  • http://doc.akka.io/docs/akka-http/current/scala/http/introduction.html

knoldus-advt-sticker


Written by 

Shubham is a Software Consultant, with experience of more than 1.5 years.He is familiar with Object Oriented Programming Paradigms. He is always eager to learn new and advance concepts. Aside from being a programmer, his hobbies include playing badminton,watching movies, writing blogs. He has experience working in C, C++, CoreJava, Adv Java, HTML, CSS, JS, Ajax.

2 thoughts on “Akka HTTP Routing

Leave a Reply

%d bloggers like this: