2016-05-01 1 views
0

Je voudrais encoder correctement URL: http://a.b.c/apis/POST /foo/barPOST /foo/bar devrait être codé comme: POST%20%2Ffoo%2Fbar.Correctement Encoder spray.http.Uri.Path?

Voici ce que j'ai essayé:

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 

Mais, les spectacles ci-dessus /foo/bar comme champs de chemin supplémentaires, plutôt que comme GET%20%2Ffoo%2Fbar.

En outre, j'ai essayé:

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

Cependant, par https://stackoverflow.com/a/2678602/409976, un espace doit être codé comme %20 dans la partie de chemin (que je comprends). En outre, lorsque vous utilisez un + au lieu de %20, le service Web auquel j'accède renvoie un HTTP-500.

Répondre

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