Scala - URL with Query String Parser and Builder DSL

theon picture theon · Nov 19, 2012 · Viewed 22.4k times · Source

In Scala how do I build up a URL with query string parameters programmatically?

Also how can I parse a String containing a URL with query string parameters into a structure that allows me to edit the query string parameters programmatically?

Answer

theon picture theon · Oct 26, 2013

Spray has a very efficient URI parser. Usage is like so:

import spray.http.Uri
val uri = Uri("http://example.com/test?param=param")

You can set the query params like so:

uri withQuery ("param2" -> "2", "param3" -> 3")