You need to sign in to do that
Don't have an account?
EncodingUtil.urlEncode method not returning what I expected
I am trying to use the method EncodingUtil.urlEncode as shown below to encode a string passed into my method 'checkProduct'. For example, if I call checkProduct('Jelly Beans'), I am expecting that the econdedURL would equal https://somewhere.com/something/Jelly%20Beans. I am not getting that. I am getting https://somewhere.com/something/Jelly++Beans.
What I need is for any of the spaces to be converted to %20. I thought this is what the urlEncode method would do? Any help would be appreciated.
public String checkProduct(String target) { String URL = 'https://somewhere.com/something/'; String encodedURL; System.debug('URL= ' + URL); encodedURL = URL + EncodingUtil.urlEncode(target, 'UTF-8'); System.debug('encodedURL= ' + encodedURL); )
+ should work the same as %20 in a urlEncoded string. Example:
http://www.google.com/search?q=Book+Worm
http://www.google.com/search?q=Book%20Worm
Is something actually breaking when you attempt to use the +'d URL?
The problem is the '+' can be used in the string and is a valid character to search on. For example, if I passed 'Jelly+Beans', it doesn't mean 'Jelly Beans' to the server it means 'Jelly+Beans'.
I used the replace method on the string to strip out the spaces and change them to %20, but was trying to avoid other encoding issues with special characters so this is why I was trying to use urlEncode.
Please let me know if there is any other way.
Thanks
Simon, i am facing the same issue. I am using oAuth authentication and it expects space as %20 and + as %2B.
If i try to send space encoded as +, it rejects my authentication.
Is there a way in APEX that i can encode the string which gives me the above values. The last option i see is replacing the spaces with %20.
I tried with contastValue.replaceAll( ' ', '%20 ');, it worked for me.
The space character is a reserved character and must be encoded as "%20" (and not as "+").
So providing an option for encoding using "%20" would be quite useful. As it is, I had to write my own UriEncode function.
Reference: http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
e.g.
String messageData = "abc , 123, \n\n\n abc123";
messageData = (String)EncodingUtil.urlEncode(messageData, 'UTF-8').replaceAll('\\+','%20');