From: Mark Hubbart Date: 2004-07-15T08:39:22+09:00 Subject: Re: How to encode binary for http.post() On Jul 14, 2004, at 12:33 PM, Randy Lawrence wrote: > Mark Hubbart wrote: > >> On Jul 5, 2004, at 2:43 PM, Randy Lawrence wrote: >>> I'm having problems posting a binary string (around 256 characters) >>> using http.post(). >>> >>> When I encode the binary string using Base64.encode64() and then >>> http.post() the data, the "+" characters are replaced by " " so the >>> data is no longer valid. >>> >>> I've tried URI.escape(Base64(encode64(data))) to post and >>> URI.unescape(data) to receive but the same problem appears. >>> >>> Any suggestions? >> The rfc that describes the base64 encoding has a slightly different >> version of the encoding that is url/filename-safe. It is rather >> simple to tr the characters around to get the right results: >> http://www.faqs.org/rfcs/rfc3548.html >> something like this: >> require 'base64' >> => nil >> def Base64.encode64url(str) >> Base64.encode64(str).tr("+/","-_") >> end >> => nil >> def Base64.decode64url(str) >> Base64.decode64(str.tr("-_","+/")) >> end >> => nil >> <...snip...> > I ran into problems precisely because of this issue. I had "+" > showing up as " ". > > I simply replace spaces in Base64 encoded string to "+" as a > workaround but would love something more appropriate/standard to use. I'm not sure what you mean... those two functions, added to the Base64 module, should solve the issue. They just implement more of the Base64 standard. The RFC describes base64 encoding, then it gives an alternative encoding for use in urls and filenames, where the + and / are replaced with - and _. cheers, Mark