Question: I am trying to use URI to create an uri, but it seems that brackets ([]) are not accepted:

```
uri = URI.parse("http://example.com")
uri.path = URI.escape("/test[1].png")

URI::InvalidComponentError: bad component(expected absolute path component): /test[1].png
from /Users/something/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/uri/generic.rb:823:in `check_path'

```

Any idea how to do? Should I just not use URI and just escape the uri at the end?

Answer: 
I'd recommend working with [Addressable::URI](http://addressable.rubyforge.org/) for this.

While Ruby's built-in URI is convenient, it is showing some signs of age, and I suspect this is one. Addressable::URI has been proposed as a replacement for URI because it conforms with current RFCs and is more full-featured.

```
require 'addressable/uri'
Addressable::URI.parse("http://example.com/test[1].png")
=> #<Addressable::URI:0x81404710 URI:http://example.com/test[1].png>

Addressable::URI.parse("http://example.com/test[1].png").to_s
=> "http://example.com/test[1].png"

```

For simple, generic, uses I use URI, just because. For heavy-lifting I use Addressable::URI.
URI.escape takes additional parameter (string or regex, default is REGEXP::UNSAFE) to customize characters that you want to escape.

Try this:

```
uri = URI.parse("http://example.com")
uri.path = URI.escape("/test[1].png", "[]")

```

The URI::InvalidComponentError is because square brackets are not allowed in URIs, except for certain places. See [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986):

---

3.2.2. Host

<...>

A host identified by an Internet Protocol literal address, version 6 [RFC3513] or later, is distinguished by enclosing the IP literal within square brackets ("[" and "]"). **This is the only place where square bracket characters are allowed in the URI syntax.**

---

Appendix D. Changes from RFC 2396

D.1. Additions

<...> **Square brackets are now specified as reserved within the authority component and are not allowed outside their use as delimiters for an IP literal within host.**