我想正确地URL编码:http://a.b.c/apis/POST /foo/bar,其中POST /foo/bar应该被编码为:POST%20%2Ffoo%2Fbar。
以下是我尝试过的:
scala> import spray.http._
import spray.http._
scala> val base = Uri("http:/a.b.c")
base: spray.http.Uri = http:///a.b.c
scala> val path = Uri.Path("/apis/GET /foo/bar")
path: spray.http.Uri.Path = /apis/GET%20/foo/bar
scala> base.withPath(path)
res0: spray.http.Uri = http:///apis/GET%20/foo/bar但是,上面所示的/foo/bar是附加的路径字段,而不是GET%20%2Ffoo%2Fbar。
此外,我还试着:
scala> Uri.Path("/apis/" + java.net.URLEncoder.encode("GET /foo/bar", "UTF-8"))
res1: spray.http.Uri.Path = /apis/GET+%2Ffoo%2Fbar但是,按照https://stackoverflow.com/a/2678602/409976,在path部分(如我所理解),应该将空间编码为%20。此外,当使用+而不是%20时,我访问的web服务返回HTTP-500。
发布于 2016-05-01 23:42:45
scala> Uri("http:/a.b.c").path / "apis" / "GET /foo/bar"
res0: spray.http.Uri.Path = /a.b.c/apis/GET%20%2Ffoo%2Fbarhttps://stackoverflow.com/questions/36972681
复制相似问题