I'm trying to make an HTTP request via spray where the url contains a query parameter that is url-encoded but when decoded is not a string but a raw Array[Byte]. I'm able to use commons-codec's UrlCodec to perform the encoding that I need. Here's an example which I think explains the problem I'm having:
import akka.util.ByteString
import org.apache.commons.codec.net.URLCodec
import scala.util.Random
import spray.http.Uri
//create bytestring containing random bytes
val randomBytes = new Array[Byte](20)
Random.nextBytes(randomBytes)
val bs = ByteString.fromArray(randomBytes)
//url encode it with URLCodec from commons-codec
val commonsEncoded = new String(new URLCodec("UTF-8").encode(bs.toArray))
//commonsEncoded: %A6%BD%D8j%DA%2C%1ELs%EE%8F%B4JM+%12%E6zf%AC
//create uri with spray
val sprayEncoded = uri.toString()
I think the fundamental issue that I'm running into is that spray requires all query values to be a "not yet url-encoded string", but the value I would like to provide is not necessarily representable as a string in any character set. If there was a way to tell spray to not url-encode its parameters (as they're already encoded), or a way to specify a query parameter as a (String -> Array[Byte]) rather than as a (String -> String) I think that would resolve the issue I'm seeing.
Am I missing something in the spray api? I'm trying to write an app which implements a spec that requires this form of encoding, so it's not something I can just decide to do differently.
Thanks,
Mark.