Escaping sequence for URIs is defined in section 2.4.1 of RFC2396 (Uniform Resource Identifiers):
An escaped octet is encoded as a character triplet, consisting of the
percent character "%" followed by the two hexadecimal digits
representing the octet code. For example, "%20" is the escaped
encoding for the US-ASCII space character.
escaped = "%" hex hex
hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
"a" | "b" | "c" | "d" | "e" | "f"
This RFC also defines reserved characters for the path
component in section 3.3:
Within a path segment, the characters "/", ";", "=", and "?" are reserved.
So you need to use encodeURIComponent() because escape()
has been deprecated and encodeURI()
does not escape all reserved characters which need to be escaped as per the RFC excerpt above.
The example below shows that only encodeURIComponent()
is escaping slashes properly (these are the characters which most likely cause the problems which you are facing):
>>> escape('//');
"//"
>>> encodeURI('//');
"//"
>>> encodeURIComponent('//');
"%2F%2F"
However please note that if possible, you should use POST instead of GET. This is the correct method to use in REST (and in general), as you are sending data from the client to the server (POST) rather than getting them from the server (GET).
Using POST will also allow you to avoid an additional problem. As length of URIs is limited in common web servers, sooner or later you will encounter a request with a very long URI which either gets trimmed or throws an error. Switching to POST will allow you to keep the URI clean and keep the data in the body of the message instead of the URI. See answers to this question for details on URI length limits.