Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Parameters using Akka HTTP Directives during GET requests

I have defined methods as getJobByID and getJobByName in scala , now I am able to pass the Id parameter during the GET call as

val route = (path("dataSource"/LongNumber) & get){ id =>
  complete(getJobById(id).map(_.asJson))
}

Now I want to get all jobs by name in similar fashion but didn't found any directive which can be used to get the Job name as the parameter and use it to find all job names. Do we have any solution or work around for this?

like image 378
Vishal Avatar asked May 09 '17 11:05

Vishal


2 Answers

The Segment path matcher will extract a String value from the path and pass it on as a function argument:

val strRoute : Route = 
  get {
    path("dataSourceByName" / Segment) { jobName : String =>
      ...
    }
  }
like image 169
Ramón J Romero y Vigil Avatar answered Sep 28 '22 07:09

Ramón J Romero y Vigil


You can do it this way as well for String.

 def getRoute : Route = get {
        path("dataSourceByName") {
          parameters('name.as[String]) {
            (name) => 
              ...
           }
         }
       }

Similarly for Int instead of as[String] do as[Int]

P.S. : For 'as' to run import following statement: import akka.http.scaladsl.server.Directives._

like image 23
Ruchir Dixit Avatar answered Sep 28 '22 09:09

Ruchir Dixit