Scala - 带有查询字符串解析器和生成器DSL的URL

the*_*eon 18 url dsl parsing scala query-string

在Scala中,如何以编程方式使用查询字符串参数构建URL?

另外,如何将String包含查询字符串参数的URL 解析为允许我以编程方式编辑查询字符串参数的结构?

the*_*eon 15

Spray有一个非常有效的URI解析器.用法如下:

import spray.http.Uri
val uri = Uri("http://example.com/test?param=param")
Run Code Online (Sandbox Code Playgroud)

您可以像这样设置查询参数:

uri withQuery ("param2" -> "2", "param3" -> 3")
Run Code Online (Sandbox Code Playgroud)

  • spray-http取决于Akka :-( ...为了解析URI而拉这种依赖看起来像过度工程. (3认同)

the*_*eon 13

以下库可以帮助您使用查询字符串参数解析和构建URL(免责声明:这是我自己的库):https://github.com/lemonlabsuk/scala-uri

它提供了一个用于使用查询字符串构建URL的DSL:

val uri = "http://example.com" ? ("one" -> 1) & ("two" -> 2)
Run Code Online (Sandbox Code Playgroud)

您可以解析uri并将参数设置为Map[String,List[String]]如此:

val uri = parseUri("http://example.com?one=1&two=2").query.params
Run Code Online (Sandbox Code Playgroud)