14

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...
}
Community
  • 1
  • 1
Siva Kumar
  • 632
  • 3
  • 9
  • 19

3 Answers3

34

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)
      }
    }
  }
}
Selvaram G
  • 727
  • 5
  • 18
  • 6
    For this to work, `import akka.http.scaladsl.server.Directives._` needs to be there. – akauppi Sep 20 '17 at 11:06
  • Is it normal practice to separate key value pairs as two separate query params (as shown in your example)? The alternative being that instead of having a query param called "key" we would just call it authType and have its value be Basic345 – Janac Meena Apr 14 '19 at 15:50
  • Is it possible to give the parameters default values? – Joost Döbken Feb 23 '22 at 11:06
1

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)
  }
}
Didac Montero
  • 2,046
  • 19
  • 27
0

If you wish extract query parameters as one piece

extract(ctx => ctx.request.uri.queryString(charset = Charset.defaultCharset)) { queryParams =>
   //useyourMethod()
}
Puneeth Reddy V
  • 1,538
  • 13
  • 28