Randomize Randomize - 1 year ago 363
Scala Question

Akka: Not valid URI

I am using


for the following url/GET:

http://localhost:8000/some-service/getSomething?param={"name":"john","street":"SOMEWHERE STREET","surname":"smith"}

(yes the
is in a

but I am getting:

akka.http.scaladsl.model.IllegalUriException: Illegal URI reference: Invalid input ' ', expected raw-query-char, '#' or 'EOI'

I cannot get what is wrong with that URL. I am reading the used RFC spec here:


but still I can't figure it out. Any help?

Answer Source

As I mentioned in my comment, you need to url endode that json so that the complete url, with query params, is valid according to the RFC spec. First, let's show what's broken again. Consider that you have this code:

val json = """{"foo":"some foo"}"""
val baseUri = "http://some.host.com:8080/test" 

val akkaUri = Uri(s"$baseUri/?json=$json")

This is one way to build an Akka Uri, but this way will be broken and throw an exception as the query param is not properly encoded. If you change your code to this instead, then things will work:

val akkaUri = Uri(baseUri).withQuery(Uri.Query(Map("json" -> json)))

By using withQuery, you allow the Akka Http framework properly url encode the params, which won't happen if you just construct the Uri from the complete uri string