Reputation: 27422
Why do you need to encode URLs? Is there a good reason why you have to change every space in the GET data to %20
?
Upvotes: 71
Views: 47791
Reputation: 1
When making a request to an API, the parameters included in the URL request may contain characters that have special meaning for the web server. URL encoding allows the browser or web server to safely transfer this data , as it converts all special characters and spaces into a format that web browsers can understand.
Upvotes: 0
Reputation: 471
Let's break down your question.
Why do you need to encode URL?
A URL is composed of only a limited number of characters and those are digits(0-9), letters(A-Z, a-z), and a few special characters("-", ".", "_", "~").
So does it mean that we cannot use any other character?
The answer to this question is "YES". But wait a minute, there is a hack and the hack is URL Encoding or Perchantage Encoding.
So if you want to transmit any character which is not a member of the above mentioned (digits, letters, and special chars), then we need to encode them. And that is why we need to encode "space" as "%20".
Here are some helpful resources (hopefully):
Upvotes: 14
Reputation: 191
Hey! Check out this derping cat playing a piano!
http://www.mysite.com/?video=funny cat plays piano.
See how the link breaks?
Now look at this:
http://www.mysite.com/?video=funny%20cat%20plays%20piano.
Upvotes: 19
Reputation: 73966
Because some characters have special meanings.
For instance, in a query string, the ampersand (&
) is used as a separator between key-value pairs. If you were to put an ampersand into one of those values, it would look like the separator between the end of a value and the beginning of the next key. So for special characters like this, we use percent encoding so that we can be sure that the data is unambiguously encoded.
Upvotes: 67
Reputation: 1169
Well, you do so because every different browsers knows how the string that makes up the URL is encoded. converting the space to %20, etc makes that URL/URI portable. It could be latin-1 it could be unicode. It needs normalized to something that is understood universally. Take a look at rfc3986 https://www.rfc-editor.org/rfc/rfc3986#section-2.1
Upvotes: -2
Reputation: 799490
From RFC 2936, section 2.4.3:
The space character is excluded because significant spaces may disappear and insignificant spaces may be introduced when URI are transcribed or typeset or subjected to the treatment of word- processing programs. Whitespace is also used to delimit URI in many contexts.
Upvotes: 46