How to Encode Uri Parameter Values

How to encode URL parameters?

With PHP

echo urlencode("http://www.image.com/?username=unknown&password=unknown");

Result

http%3A%2F%2Fwww.image.com%2F%3Fusername%3Dunknown%26password%3Dunknown

With Javascript:

var myUrl = "http://www.image.com/?username=unknown&password=unknown";
var encodedURL= "http://www.foobar.com/foo?imageurl=" + encodeURIComponent(myUrl);

DEMO: http://jsfiddle.net/Lpv53/

How do I encode URI parameter values?

Jersey's UriBuilder encodes URI components using application/x-www-form-urlencoded and RFC 3986 as needed. According to the Javadoc

Builder methods perform contextual encoding of characters not permitted in the corresponding URI component following the rules of the application/x-www-form-urlencoded media type for query parameters and RFC 3986 for all other components. Note that only characters not permitted in a particular component are subject to encoding so, e.g., a path supplied to one of the path methods may contain matrix parameters or multiple path segments since the separators are legal characters and will not be encoded. Percent encoded values are also recognized where allowed and will not be double encoded.

Java URL encoding of query string parameters

URLEncoder is the way to go. You only need to keep in mind to encode only the individual query string parameter name and/or value, not the entire URL, for sure not the query string parameter separator character & nor the parameter name-value separator character =.

String q = "random word £500 bank $";
String url = "https://example.com?q=" + URLEncoder.encode(q, StandardCharsets.UTF_8);

When you're still not on Java 10 or newer, then use StandardCharsets.UTF_8.toString() as charset argument, or when you're still not on Java 7 or newer, then use "UTF-8".


Note that spaces in query parameters are represented by +, not %20, which is legitimately valid. The %20 is usually to be used to represent spaces in URI itself (the part before the URI-query string separator character ?), not in query string (the part after ?).

Also note that there are three encode() methods. One without Charset as second argument and another with String as second argument which throws a checked exception. The one without Charset argument is deprecated. Never use it and always specify the Charset argument. The javadoc even explicitly recommends to use the UTF-8 encoding, as mandated by RFC3986 and W3C.

All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used.

See also:

  • What every web developer must know about URL encoding

Encoding URL query parameters in Java

java.net.URLEncoder.encode(String s, String encoding) can help too. It follows the HTML form encoding application/x-www-form-urlencoded.

URLEncoder.encode(query, "UTF-8");

On the other hand, Percent-encoding (also known as URL encoding) encodes space with %20. Colon is a reserved character, so : will still remain a colon, after encoding.

Encode both parameter and value or just value in URL for http post?

URLEncoder.encode is called to ensure that there are no unsafe chars in your URI. an unsafe char is nearly every char which is not a letter, a digit and some special chars.

From the java-doc of the URLEncoder

The alphanumeric characters "a" through "z", "A" through "Z" and "0" through >"9" remain the same.
The special characters ".", "-", "*", and "_" remain the same.
The space character " " is converted into a plus sign "+".
All other characters are unsafe and are first converted into one or more bytes >using some encoding scheme. Then each byte is represented by the 3-character >string "%xy", where xy is the two-digit hexadecimal representation of the byte. >The recommended encoding scheme to use is UTF-8. However, for compatibility >reasons, if an encoding is not specified, then the default encoding of the >platform is used.

Example:

String query = "foo=abc&bar=def";

so. if you encode the whole query it will result in

foo%3Dabc%26bar%3Defg

In this case you also encoded the = and & which are needed to seperate the parts of the query.

You have to encode the names and value of your query to ensure that they do not contain unsafe chars. e.g. &, = and any non printable/special char. if you know, that your name only contains safe chars, you don't have to encode the name.

String param1 = URLEncoder.encode("abc", "utf-8");
String param2 = URLEncoder.encode("a&b", "utf-8");
String query = "foo=" + param1 + "&bar=" + param2;

results in

foo=abc&bar=a%26b

which should be the query you need. please note the & in param2 which will be encoded, cause it is "unsafe" for an valid url!

Encode URL in JavaScript

Check out the built-in function encodeURIComponent(str) and encodeURI(str).

In your case, this should work:

var myOtherUrl = 
"http://example.com/index.html?url=" + encodeURIComponent(myUrl);

How can I correctly encode query parameters in a url?

When dealing with HttpClient related tasks, A better approch is to use some handly library like RestSharp.

Using RestSharp NuGet library (https://www.nuget.org/packages/RestSharp) - You don't need to worry about URL encoding, JSON parsing and a lot more

var client = new RestClient("https://countries.com");
var request = new RestRequest("GetCountry", Method.GET);
// As you mentioned, If you do no need to encode URL parameters. Add the encoding off option
request.AddQueryParameter("countryName", "India", ParameterType.QueryStringWithoutEncode);
request.AddQueryParameter("cityName", "Kochi", ParameterType.QueryStringWithoutEncode);
var response = client.Execute(request);

This will give

https://countries.com/GetCountry?countryName=India&cityName=Kochi

URL encoding is automaticaly handled. You don't need to worry about it

Encode & in query parameter value in string url in java

Here is how I have solved my problem

private URI convertStringURLToURI(String url) {
URI uri = null;

UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(url);
String queryParameter = uriComponentsBuilder.build().toUri().getQuery();

if(queryParameter != null) {

// Split the query part into queries
String[] splitedQueryParameter = queryParameter.split("&");

Stack<String> queryParamStack = new Stack<>();

for(int i = 0; i < splitedQueryParameter.length; i++) {
/*
In case "&" is present in any of the query values ex. b=abc & xyz then
it would have split as "b=abc" and "xyz" due to "&" symbol

Below code handle such situation
If "=" is present in any value then it is pushed to stack,
else "=" is not present then this value was part of the previous push query
due to "&" present in query value, so the else part handles this situation
*/
if(splitedQueryParameter[i].contains("=")) {
queryParamStack.push(splitedQueryParameter[i]);
} else {
String oldValue = queryParamStack.pop();
String newValue = oldValue + "&" + splitedQueryParameter[i];
queryParamStack.push(newValue);
}
}

MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
while(!queryParamStack.isEmpty()) {
String[] query = queryParamStack.pop().split("=");
String queryParameterValue = query[1];
/*
If in the query value, "=" is present somewhere then the query value would have
split into more than two parts, so below for loop handles that situation
*/
for(int i = 2; i < query.length; i++) {
queryParameterValue = queryParameterValue + "=" + query[i];
}
// encoding the query value and adding to Map
queryParams.add(query[0], UriUtils.encode(queryParameterValue, "UTF-8"));
}

uriComponentsBuilder.replaceQueryParams(queryParams);

try {
uri = new URI(uriComponentsBuilder.build().toString());
} catch (URISyntaxException e) {
e.printStackTrace();
}

}
return uri;
}

So, this String URL

String url = "http://www.example.com/site?a=abc&b=qwe & asd&c=foo ?bar=2";

becomes

URI uri = "http://www.example.com/site?c=foo%20%3Fbar%3D2&b=qwe%20%26%20asd&a=abc"

Here, the sequence of query gets changed due to the use of Stack, it will not cause any issue in executing the uri, however you can handle that by modifying the code

Encoding parameters for a URL

I would recommend Uri.EscapeDataString instead of using HttpUtility functions. See discussion in Server.UrlEncode vs. HttpUtility.UrlEncode.



Related Topics



Leave a reply



Submit