What constitutes a valid URI query parameter key?

I'm looking over Section 3.4 of RFC 3986 trying to understand what constitutes a valid URI query parameter key, but I'm not seeing a clear answer.

The reason I'm asking is because I'm writing a Ruby class that composes a URI with query parameters. When a new parameter is added I want to validate the key. Based on experience, it seems like the key will be invalid if it requires any escaping.

I should also say that I plan to validate the key. I'm not sure how to go about validating this data either, but I do know that in all cases I should escape this value.

Advice is appreciated. Advice in the context of how validation might already be possible through say a Ruby Gem would also be a plus.


I could well be wrong, but that spec seems to say that anything following '?' or '#' is valid as long. I wonder if you should be looking more at the spec for 'application/x-www-form-urlencoded' (ie. the key/value pairs we're all used to)?

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1

This is the default content type. Forms submitted with this content type must be encoded as follows:

Control names and values are escaped. Space characters are replaced by +', and then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by %HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (ie, `%0D%0A').

The control names/values are listed in the order they appear in the document. The name is separated from the value by =' and name/value pairs are separated from each other by &'.


I don't believe key=value is part of the RFC, it's a convention that has emerged. Wikipedia suggests this is an 'W3C recommendation'.

Seems like some good stuff to be found searching on the application/x-www-form-urlencoded content type.

  • http://www.w3.org/TR/REC-html40/interact/forms.html#form-data-set
  • 链接地址: http://www.djcxy.com/p/17590.html

    上一篇: 如何从JavaScript中检索GET参数?

    下一篇: 构成有效的URI查询参数密钥的是什么?