Saturday, June 29, 2013

What Every Web Developer Must Know About URL Encoding

Stéphane Épardaud (via Ilya Grigorik):

This article describes common misconceptions about Uniform Resource Locator (URL) encoding, then attempts to clarify URL encoding for HTTP, before presenting frequent problems and their solutions.

For example:

This means that the “blue+light blue” string has to be encoded differently in the path and query parts: “http://example.com/blue+light%20blue?blue%2Blight+blue”. From there you can deduce that encoding a fully constructed URL is impossible without a syntactical awareness of the URL structure.

and:

The syntax of the URL is only meaningful before it is URL-decoded: after URL-decoding, reserved characters may appear.

Update (2013-07-13): Mike Abdullah mentions KSURLComponents, which can help with this.

Update (2013-07-16): Mike Abdullah:

But what if you want to programmatically construct a URL containing query parameters? Don’t worry, I’ve got your back again, with KSURLQueryUtilities. There you’ll find a selection of methods to go between URL (and their strings), and NSDictionary.

Update (2013-08-14): Mike Abdullah:

Most importantly, note the use of : characters in the example above. One is used after the brackets to indicate the port number. But what about this within the brackets? As we saw above, : characters must usually be escaped as part of a hostname, but for literal addresses, the brackets act as an exception to this rule.

[…]

For further complication, websites can make use of international domain names, whereby characters outside of the regular ASCII set are encoded using a system known as punycode. e.g. the domain exämple.com is actually addressed as xn--exmple-cua.com. Web browsers take care of hiding this detail from users by presenting (and accepting) the unicode form of such domains.

2 Comments RSS · Twitter


[...] of great stuff, including Base64, NSURLComponents (sorely needed), blink and smile detection, barcode scanning, and access to Safari’s Reading List. [...]


[…] of great stuff, including Base64, NSURLComponents (sorely needed), blink and smile detection, barcode scanning, and access to Safari’s Reading List. […]

Leave a Comment