简体   繁体   English

如何在akka-http中读取查询参数?

[英]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. 我知道akka-http库在处理请求时对类类型进行编组和解组。但是现在,我需要读取GET请求的请求参数。 I tried parameter() method and It is returning ParamDefAux type but i need those values as strings types 我尝试了parameter()方法,它返回ParamDefAux类型,但我需要这些值作为字符串类型

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) 使用Akka HTTP查询GET请求的参数(正式名称为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...
}

For a request like http://host:port/path?key=authType&value=Basic345 try 对于像http://host:port/path?key=authType&value=Basic345尝试

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

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

If you wish extract query parameters as one piece 如果您希望将query parameters提取为一个整体

extract(ctx => ctx.request.uri.queryString(charset = Charset.defaultCharset)) { queryParams =>
   //useyourMethod()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM