使用 Akka HTTP(正式名称为 Spray)的 GET 请求的查询参数

编程入门 行业动态 更新时间:2024-10-09 05:16:16
本文介绍了使用 Akka HTTP(正式名称为 Spray)的 GET 请求的查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Akka HTTP(正式名称为 Spray)的功能之一是它能够自动将数据从 json 来回编组和解组到 case 类等中.我已经成功地让它正常工作.

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.

目前,我正在尝试制作一个使用查询参数执行 GET 请求的 HTTP 客户端.目前的代码如下所示:

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

好吧,那不是那么漂亮.如果我能传入一个包含查询参数的 case 类,并让 Akka HTTP 自动生成查询参数,就像它对 json 所做的那样,那就太好了.(另外,Akka HTTP 的服务器端有一种解析 GET 查询参数的优雅方式,所以人们会认为它也会有一种优雅的方式来生成它们.)

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

我想做如下事情:

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.

我想要使用 Akka HTTP 以某种方式可行吗?还是我只需要以老式的方式做事?即,显式生成查询参数,就像我在上面的第一个代码块中所做的那样.

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.

(我知道如果我将它从 GET 更改为 POST,我可能会让它更像我希望它工作的那样工作,从那时起我可以自动转换 POST 请求的内容从案例类到 json,但我真的不希望在这里这样做.)

(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.)

推荐答案

您可以利用 Uri 类来做您想做的事.它提供了多种方法来使用 withQuery 方法将一组参数放入查询字符串中.例如,您可以执行以下操作:

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

HttpRequest(Uri(hostAndPath).withQuery(("foo" -> "bar"), ("hello" -> "world")))

更多推荐

使用 Akka HTTP(正式名称为 Spray)的 GET 请求的查询参数

本文发布于:2023-11-25 10:19:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629366.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   正式   Akka   HTTP   Spray

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!