scala 如何读取akka-http中的查询参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39763652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read query parameters in akka-http?
提问by Siva Kumar
I know akka-http libraries marshal and unmarshal to class type while processing request.But now, I need to read request-parameters of GETrequest. I tried parameter()method and It is returning ParamDefAuxtype but i need those values as strings types
我知道 akka-http 库在处理请求时对类类型进行编组和解组。但是现在,我需要读取请求的请求参数GET。我试过parameter()方法,它正在返回ParamDefAux类型,但我需要这些值作为字符串类型
I check for answer at below questions.
我检查以下问题的答案。
How can I parse out get request parameters in spray-routing?
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
请告诉我如何从请求中提取查询参数。或我如何从中提取所需的值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...
}
回答by Selvaram G
For a request like http://host:port/path?key=authType&value=Basic345try
对于像http://host:port/path?key=authType&value=Basic345尝试这样的请求
path("path") {
get {
parameters('key.as[String], 'value.as[String]) { (key, value) =>
complete {
someFunction(key,value)
}
}
}
}
回答by Didac Montero
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)
}
}
回答by Puneeth Reddy V
If you wish extract query parametersas one piece
如果您希望提取query parameters为一件
extract(ctx => ctx.request.uri.queryString(charset = Charset.defaultCharset)) { queryParams =>
//useyourMethod()
}

