# Alternate URI escaping mechanisms?

**URL:** <https://www.ruby-forum.com/t/alternate-uri-escaping-mechanisms/82041>\
**Category:** Ruby\
**Created:** [January 17, 2007, 12:24am UTC](https://www.ruby-forum.com/t/alternate-uri-escaping-mechanisms/82041 "2007-01-17T00:24:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwfrank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f1d935/32.png) [@dwfrank](https://www.ruby-forum.com/u/dwfrank)\
**Post date:** [January 17, 2007, 12:24am UTC](https://www.ruby-forum.com/t/alternate-uri-escaping-mechanisms/82041/1 "2007-01-17T00:24:09Z")

</div>

URI#escape escapes to UTF-8. I need to escape to Latin-1 for Amazon’s  
REST API. Is there another library I can use? There doesn’t seem to be  
a way to change what URI#escape uses (at least, not from the skeleton  
std API docs).

I’m getting very little to go on via searching.

–dwf

---

<div class="post-metadata">

**Author:** ![dwfrank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f1d935/32.png) [@dwfrank](https://www.ruby-forum.com/u/dwfrank)\
**Post date:** [January 17, 2007, 12:31am UTC](https://www.ruby-forum.com/t/alternate-uri-escaping-mechanisms/82041/2 "2007-01-17T00:31:17Z")

</div>

Davis Frank wrote:

> URI#escape escapes to UTF-8. I need to escape to Latin-1 for Amazon’s  
> REST API. Is there another library I can use? There doesn’t seem to be  
> a way to change what URI#escape uses (at least, not from the skeleton  
> std API docs).
> 
> I’m getting very little to go on via searching.

Iconv may be what you’re after…

---

<div class="post-metadata">

**Author:** ![dwfrank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f1d935/32.png) [@dwfrank](https://www.ruby-forum.com/u/dwfrank)\
**Post date:** [January 17, 2007, 12:43am UTC](https://www.ruby-forum.com/t/alternate-uri-escaping-mechanisms/82041/3 "2007-01-17T00:43:17Z")

</div>

Davis Frank wrote:

> URI#escape escapes to UTF-8. I need to escape to Latin-1 for Amazon’s  
> REST API. Is there another library I can use? There doesn’t seem to be  
> a way to change what URI#escape uses (at least, not from the skeleton  
> std API docs).
> 
> I’m getting very little to go on via searching.

URI#escape escapes to UTF-8 because your string is UTF-8-encoded. Use  
iconv, as Alex suggested, to re-encode it to Latin-1, or do it with  
unpack and pack  
str.unpack(“U\*”).pack(“C\*”)

and then apply URI#escape.

Of course, both methods will fail if any character in your string is  
outside the Latin-1 range.

Good luck.

---

<div class="post-metadata">

**Author:** ![dwfrank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f1d935/32.png) [@dwfrank](https://www.ruby-forum.com/u/dwfrank)\
**Post date:** [January 17, 2007, 12:52am UTC](https://www.ruby-forum.com/t/alternate-uri-escaping-mechanisms/82041/4 "2007-01-17T00:52:18Z")

</div>

Carlos wrote:

> > URI#escape escapes to UTF-8. I need to escape to Latin-1 for Amazon’s  
> > […]  
> > URI#escape escapes to UTF-8 because your string is UTF-8-encoded. Use  
> > […]  
> > and then apply URI#escape.

Correction: it’s not URI#escape but URI.escape.

---

<div class="post-metadata">

**Author:** ![dwfrank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f1d935/32.png) [@dwfrank](https://www.ruby-forum.com/u/dwfrank)\
**Post date:** [January 17, 2007, 2:05am UTC](https://www.ruby-forum.com/t/alternate-uri-escaping-mechanisms/82041/5 "2007-01-17T02:05:21Z")

</div>

Carlos wrote:

> Carlos wrote:
> 
> > > URI#escape escapes to UTF-8. I need to escape to Latin-1 for Amazon’s  
> > > […]  
> > > URI#escape escapes to UTF-8 because your string is UTF-8-encoded. Use  
> > > […]  
> > > and then apply URI#escape.

I found Iconv, but my head was bent the wrong way. I was escaping, then  
converting instead of converting, then escaping.

The magic is here:  
URI.escape(Iconv.new(‘latin1’, ‘utf-8’).iconv(value))

Now everything is happy. Thanks for the quick response.

–dwf
