function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

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  I am not getting that.  I am getting


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 = ''; 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:


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.



The server is broken, the spec says + is the correct encoding of a space character (and that a real + character should be % encoded). If you can't get the server fixed, then you'll need to write your own version of the encoding function.

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.

Not sure this message is for me.
Hi, even I faced the same issue while encoding a constant value which is to be appened into my end point. I tried to do EncodingUtil.Urlencode(target, 'UTF-8'), but it was giving ' + ' when I saw the debug message for endpoint.
I tried with  contastValue.replaceAll( '  ', '%20 ');, it worked for me.

Sean Carter 3Sean Carter 3
Just a note, the specification for authenticating requests in Amazon S3 states the following:

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.

Barry PericBarry Peric
I was having a similar issues with linebreaks causing failed posts. The following replaceall fixed it without needing to write anything custom.

String messageData = "abc , 123, \n\n\n abc123";

messageData = (String)EncodingUtil.urlEncode(messageData, 'UTF-8').replaceAll('\\+','%20');