How to read query parameters in akka-http?

Siva Kumar picture Siva Kumar · Sep 29, 2016 · Viewed 14.3k times · Source

I know akka-http libraries marshal and unmarshal to class type while processing request.But now, I need to read request-parameters of GET request. I tried parameter() method and It is returning ParamDefAux type but i need those values as strings types

I check for answer at below questions.

  1. How can I parse out get request parameters in spray-routing?

  2. Query parameters for GET requests using Akka HTTP (formally known as Spray)

but can't do what i need.

Please tell me how can i extract query parameters from request. OR How can I extract required value from ParamDefAux

Request URL

http://host:port/path?key=authType&value=Basic345

Get method definition

 val  propName = parameter("key")
 val  propValue = parameter("value")
 complete(persistanceMgr.deleteSetting(propName,propValue))

My method declarations

def deleteSetting(name:String,value:String): Future[String] = Future{
 code...
}

Answer

Selvaram G picture Selvaram G · Sep 29, 2016

For a request like http://host:port/path?key=authType&value=Basic345 try

path("path") {
  get {
    parameters('key.as[String], 'value.as[String]) { (key, value) =>
      complete {
        someFunction(key,value)
      }
    }
  }
}