Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...
}
like image 719
Siva Kumar Avatar asked Sep 29 '16 07:09

Siva Kumar


2 Answers

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)
      }
    }
  }
}
like image 149
Selvaram G Avatar answered Sep 27 '22 22:09

Selvaram G


Even though being less explicit in the code, you can also extract all the query parameters at once from the context. You can use as follows:

// Previous part of the Akka HTTP routes ...
extract(_.request.uri.query()) { params  =>
  complete {
    someFunction(key,value)
  }
}
like image 36
Didac Montero Avatar answered Sep 27 '22 23:09

Didac Montero