Kevin Meredith
Kevin Meredith

Reputation: 41909

Properly Encode spray.http.Uri.Path?

I'd like to properly URL encode: http://a.b.c/apis/POST /foo/bar where POST /foo/bar should be encoded as: POST%20%2Ffoo%2Fbar.

Here's what I tried:

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

But, the above shows /foo/bar as additional path fields, rather than as GET%20%2Ffoo%2Fbar.

In addition, I tried:

scala> Uri.Path("/apis/" + java.net.URLEncoder.encode("GET /foo/bar", "UTF-8"))
res1: spray.http.Uri.Path = /apis/GET+%2Ffoo%2Fbar

However, per https://stackoverflow.com/a/2678602/409976, a space should be encoded as %20 in the path part (as I understand). In addition, when using a + rather than %20, the web service that I'm accessing returns an HTTP-500.

Upvotes: 0

Views: 552

Answers (1)

Brian Kent
Brian Kent

Reputation: 3854

scala> Uri("http:/a.b.c").path / "apis" / "GET /foo/bar"
res0: spray.http.Uri.Path = /a.b.c/apis/GET%20%2Ffoo%2Fbar

Upvotes: 2

Related Questions