Siva Kumar Siva Kumar - 2 months ago 28
Scala Question

How to read query parameters in akka-http?

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

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)
      }
    }
  }
}
Comments