Url: https://myproject.dev.com/methodology/Culture?contentName=abc & def&path=Test&type=folder
Need to fetch only query params from above URL but problem is '&' in contentName=abc & def so while fetching the contentName getting the value in two parts like abc, def.
Please suggest the approach to get contentName is abc & def instead of abc,def.
CodePudding user response:
If we pass any type of special character we have to encode those values. Java provides a URLEncoder class for encoding any query string or form parameter into URL encoded format. When encoding URI, one of the common pitfalls is encoding the complete URI. Typically, we need to encode only the query portion of the URI.
Map<String, String> requestParameters = new HashMap<>();
requestParameters.put("keyA", "abc123");
requestParameters.put("keyB", "val@!&$2");
String encodedURL = requestParameters.keySet().stream()
.map(key -> key "=" encodeValue(requestParameters.get(key)))
.collect(joining("&", "http://www.url_needed.com?", ""));
private String encodeValue(String value) {
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
}
CodePudding user response:
If the & character is part of the name or value in the query string then it has to be percent encoded. In the given example: contentName=abc&def&path=Test&type=folder.
