Special characters and delimiters
When handling URL values for queries, it's essential to encode any special characters to ensure that they're correctly interpreted. However, don't encode any important delimiters used in the URL.
Practical Steps for Encoding
-
Identify special characters. Look for spaces, punctuation marks, and non-ASCII characters in your URL components.
-
Use encoding tools: Use online tools or built-in functions in your programming language to encode these characters.
The following is a simple guide to help you understand what to encode and what to leave as is.
What to encode
Special characters include spaces, punctuation marks, and non-ASCII characters. These characters can cause issues in URLs if you don't properly encode them.
Common special characters and their encoded equivalents:
Name |
Appearance |
Encode value |
---|---|---|
Space |
|
%20 |
Exclamation mark |
! |
%21 |
Hash |
# |
%23 |
Dollar sign |
$ |
%24 |
Ampersand |
& |
%26 |
Plus sign |
+ |
%2B |
Comma |
, |
%2C |
Slash |
/ |
%2F |
Colon |
: |
%3A |
Semicolon |
; |
%3B |
Equal sign |
= |
%3D |
Question mark |
? |
%3F |
At sign |
@ |
%40 |
What not to encode
Delimiters are essential parts of the URL structure and you shouldn't encode them.
Name |
Example |
---|---|
Scheme or protocol |
https:// |
Domain |
api.example |
Path separator |
/ |
Query parameter start |
? |
Query parameter separator |
& |
Examples
Correct encoding ensures that special characters are properly represented without affecting the delimiters.
-
Original URL: https://api.example.com/v1/users?name=John Doe&age=30&city=New York
-
Encoded URL: https://api.example.com/v1/users?name=John%20Doe&age=30&city=New%20York
In this example, the spaces in John Doe and New York are encoded as %20. The delimiters (://, /, ?, &, and =) remain unchanged.
Incorrectly encoding delimiters can lead to errors.
-
Encoded URL: https%3A%2F%2Fapi.example.com%2Fv1%2Fusers%3Fname%3Djohn%26age%3D30
In this example, encoding the : and / characters from the https:// protocol and the ? and & characters that are part of the query parameters makes the URL invalid.