Scala - 带有查询字符串解析器和构建器 DSL 的 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13463387/
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
Scala - URL with Query String Parser and Builder DSL
提问by theon
In Scala how do I build up a URL with query string parameters programmatically?
在 Scala 中,如何以编程方式使用查询字符串参数构建 URL?
Also how can I parse a Stringcontaining a URL with query string parameters into a structure that allows me to edit the query string parameters programmatically?
另外,如何将String包含带有查询字符串参数的 URL解析为允许我以编程方式编辑查询字符串参数的结构?
采纳答案by theon
The following library can help you parse and build URLs with query string parameters (Disclaimer: This is my own library): https://github.com/lemonlabsuk/scala-uri
以下库可以帮助您使用查询字符串参数解析和构建 URL(免责声明:这是我自己的库):https: //github.com/lemonlabsuk/scala-uri
It provides a DSL for building URLs with query strings:
它提供了一个 DSL,用于使用查询字符串构建 URL:
val uri = "http://example.com" ? ("one" -> 1) & ("two" -> 2)
You can parse a uri and get the parameters into a Map[String,List[String]]like so:
您可以解析 uri 并将参数转换为Map[String,List[String]]如下所示:
val uri = parseUri("http://example.com?one=1&two=2").query.params
回答by theon
Spray has a very efficient URI parser. Usage is like so:
Spray 有一个非常高效的 URI 解析器。用法是这样的:
import spray.http.Uri
val uri = Uri("http://example.com/test?param=param")
You can set the query params like so:
您可以像这样设置查询参数:
uri withQuery ("param2" -> "2", "param3" -> 3")
回答by ryryguy
Theon's library looks pretty nice. But if you just want a quickie encode method, I have this one. It deals with optional parameters, and also will recognize JsValues from spray-jsonand compact print them before encoding. (Those happen to be the two things I have to worry about, but you could easily extend the match block for other cases you want to give special handling to)
Theon 的图书馆看起来很不错。但如果你只想要一种快速编码方法,我有这个。它处理可选参数,并且还会识别来自spray-json 的JsValues并在编码前压缩打印它们。(那些恰好是我必须担心的两件事,但是您可以轻松地将匹配块扩展到您想要对其进行特殊处理的其他情况)
import java.net.URLEncoder
def buildEncodedQueryString(params: Map[String, Any]): String = {
val encoded = for {
(name, value) <- params if value != None
encodedValue = value match {
case Some(x:JsValue) => URLEncoder.encode(x.compactPrint, "UTF8")
case x:JsValue => URLEncoder.encode(x.compactPrint, "UTF8")
case Some(x) => URLEncoder.encode(x.toString, "UTF8")
case x => URLEncoder.encode(x.toString, "UTF8")
}
} yield name + "=" + encodedValue
encoded.mkString("?", "&", "")
}
回答by Ashalynd
Dispatch hasn't been mentioned yet.
还没有提到派遣。
http://dispatch.databinder.net/Dispatch.html
http://dispatch.databinder.net/Dispatch.html
val myRequest = "http://somehost.com" / "some" / "path" <<? Map("id" -> "12345")
回答by tfh
also useful: https://github.com/mobiworx/urlifier
也有用:https: //github.com/mobiworx/urlifier
val url = (http || "some-domain".de) ? german & version(1) & foobar
url.toString

