encoding binary data in url parameter

1,098 views
Skip to first unread message

Mark Drago

unread,
Dec 28, 2013, 12:15:03 PM12/28/13
to spray...@googlegroups.com
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 uri = Uri("http://example.com/path?query=" + commonsEncoded)
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.

Mathias Doenitz

unread,
Dec 29, 2013, 2:50:09 AM12/29/13
to spray...@googlegroups.com
Mark,

I understand the problem and think that, when completed, this ticket would fix it for your use case:
https://github.com/spray/spray/issues/654

Can you confirm that implementing client-side support for the `Raw-Request-URI` header would indeed enable your application to function correctly?

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray.io User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Mathias Doenitz

unread,
Dec 29, 2013, 2:58:11 AM12/29/13
to spray...@googlegroups.com
Mark,

I understand the problem and think that, when completed, this ticket would fix it for your use case:
https://github.com/spray/spray/issues/654

Can you confirm that implementing client-side support for the `Raw-Request-URI` header would indeed enable your application to function correctly?

Cheers,
Mathias

---
mat...@spray.io
http://spray.io

On 28.12.2013, at 18:15, Mark Drago <mark...@gmail.com> wrote:

Mark Drago

unread,
Dec 29, 2013, 7:33:58 AM12/29/13
to spray...@googlegroups.com
Mathias,

Yep - that absolutely looks like it would solve the problem I'm experiencing.

Thanks for getting back to me so quickly,
Mark.
Reply all
Reply to author
Forward
0 new messages