Akka HTTP(正式称为Spray)的功能之一就是它能够自动将数据从json来回封送和解封到case类等。我已经成功地使它能够正常工作。
此刻,我正在尝试使一个HTTP客户端执行带有查询参数的GET请求。当前代码如下所示:
val httpResponse:Future [HttpResponse] = Http()。singleRequest(HttpRequest ( uri = s http:// $ {config.getString( http.serverHost)}:$ {config.getInt( http.port)} / + s query?seq = $ {seq} + s& max-mismatches = $ {maxMismatches} + s&pam-policy = $ {pamPolicy}))好吧,那不是很漂亮。如果我可以传入一个包含查询参数的case类,并让Akka HTTP自动生成查询参数,就像对json一样,那将是很好的。 (此外,Akka HTTP的服务器端也有一种解析GET查询参数的方法,因此,人们可能认为它也有一种生成它们的方法。)
我想执行以下操作:
val httpResponse:Future [HttpResponse] = Http()。singleRequest(HttpRequest( uri = s http:// $ {config.getString( http.serverHost)}:$ {config.getInt( http.port)} //查询,实体= QueryParams(seq = seq,maxMismatches = maxMismatches,pamPolicy = pamPolicy)))仅上述内容实际上无效。
我想用Akka HTTP以某种方式实现吗?还是我只需要用老式的方式做事?即,像上面第一个代码块一样,显式生成查询参数。
(我知道,如果我将其从GET更改为POST,我可能可以使它更像我希望的那样工作,因为从那时起,我可以将POST请求的内容自动地从case类转换为json,但是我真的不希望在这里这样做。)
解决方案您可以利用 Uri 类来执行所需的操作。它提供了多种使用 withQuery 方法将一组参数获取到查询字符串中的方法。例如,您可以执行以下操作:
val params = Map( foo-> bar, hello-> world) HttpRequest(Uri(hostAndPath).withQuery(params))Or
HttpRequest(Uri(hostAndPath).withQuery(( foo-> bar), ( hello-> world)))
One of the features of Akka HTTP (formally known as Spray) is its ability to automagically marshal and unmarshal data back and forth from json into case classes, etc. I've had success at getting this to work well.
At the moment, I am trying to make an HTTP client that performs a GET request with query parameters. The code currently looks like this:
val httpResponse: Future[HttpResponse] = Http().singleRequest(HttpRequest( uri = s"""${config.getString("http.serverHost")}:${config.getInt("http.port")}/""" + s"query?seq=${seq}" + s"&max-mismatches=${maxMismatches}" + s"&pam-policy=${pamPolicy}"))Well, that's not so pretty. It would be nice if I could just pass in a case class containing the query parameters, and have Akka HTTP automagically generate the query parameters, kind of like it does for json. (Also, the server side of Akka HTTP has a somewhat elegant way of parsing GET query parameters, so one would think that it would also have a somewhat elegant way to generate them.)
I'd like to do something like the following:
val httpResponse: Future[HttpResponse] = Http().singleRequest(HttpRequest( uri = s"""${config.getString("http.serverHost")}:${config.getInt("http.port")}/query""", entity = QueryParams(seq = seq, maxMismatches = maxMismatches, pamPolicy = pamPolicy)))Only, the above doesn't actually work.
Is what I want doable somehow with Akka HTTP? Or do I just need to do things the old-fashioned way? I.e, generate the query parameters explicitly, as I do in the first code block above.
(I know that if I were to change this from a GET to a POST, I could probably to get it to work more like I would like it to work, since then I could get the contents of the POST request automagically converted from a case class to json, but I don't really wish to do that here.)
解决方案You can leverage the Uri class to do what you want. It offers multiple ways to get a set of params into the query string using the withQuery method. For example, you could do something like this:
val params = Map("foo" -> "bar", "hello" -> "world") HttpRequest(Uri(hostAndPath).withQuery(params))Or
HttpRequest(Uri(hostAndPath).withQuery(("foo" -> "bar"), ("hello" -> "world")))
更多推荐
使用Akka HTTP(正式称为Spray)的GET请求的查询参数
发布评论