Randomize
Randomize

Reputation: 9103

Akka: Not valid URI

I am using

akka.http.scaladsl.model.Uri

for the following url/GET:

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

(yes the json is in a get/query param)

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:

https://www.rfc-editor.org/rfc/rfc3986#section-4.1

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

Upvotes: 0

Views: 2414

Answers (1)

cmbaxter
cmbaxter

Reputation: 35443

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

Upvotes: 5

Related Questions