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
Dev TNOPPV5Dev TNOPPV5 

How to encode ' ' ( Space ) to %20 ?

Hello,

I am calling one rest api in salesforce. Following is the deatil code:

 HttpRequest req = new HttpRequest();
                req.setEndpoint('https://example.com'+lo[0].Company__c);
                req.setMethod('GET');
                req.setHeader('ACCEPT','application/xml');
                req.setHeader('Authorization', authorizationHeader);
                Http http = new Http();
                HTTPResponse res = http.send(req);
                system.debug('res:'+res);

In above code lo[0].Comapny__c value is " Test Example " . In this name there is space between Test and Example. When My code execute for this example it is given me blank response body.

I am passing like "TestExample" it is working. Actually when space came at that time "%20" should pass in that url. 

I know how to do in .net , PHP, JAva ( line encodeURI("Test Example)"). 

How can i do this in Salesforce? Please help me.
Suraj GharatSuraj Gharat
Hey,

If you are looking for URL encoding, there is this utility "EncodingUtil" class into the Apex API that you can make use of as:
EncodingUtil.urlEncode('Test Example', 'UTF-8')
For more details - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_encodingUtil.htm (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_encodingUtil.htm" target="_blank)
Dev TNOPPV5Dev TNOPPV5
Hello Suraj,

I tried that but when is do like this:
 
EncodingUtil.urlEncode('Test Example', 'UTF-8')

result comes like "Test+Example"

Result I am expecting like: "Test%20Example"
Dev TNOPPV5Dev TNOPPV5
EncodingUtil.urlEncode('Test Example', 'UTF-8') 's result is : Test+Example

Result should be like: Test%20Example

I did not resolve this using above solution. 
Suraj GharatSuraj Gharat
It seems encoding space as "+" symbol but not as its Unicode character code point (%20), is becoming standard since modern browsers also do it in that way and thus above Apex API method.

However, if still your endpoint expect it to be %20 only, you need to handle it like special case, something like:
EncodingUtil.urlEncode('Test Example', 'UTF-8').replaceAll('\\+','%20');
Not recommended, though, you can make it done!
pranab khatuapranab khatua
                String url = EncodingUtil.urlEncode('https://example.com'+lo[0].Company__c, 'UTF-8')
                HttpRequest req = new HttpRequest();
                req.setEndpoint(url);
                req.setMethod('GET');
                req.setHeader('ACCEPT','application/xml');
                req.setHeader('Authorization', authorizationHeader);
                Http http = new Http();
                HTTPResponse res = http.send(req);
                system.debug('res:'+res);

 
Dev TNOPPV5Dev TNOPPV5
Hello Pranab,

It is not working. I tried this yesterday. Thanks for help. Please let me know if you have any other solution.
 
Madhav GoyalMadhav Goyal
Hi All,

Did anyone found the solution to this?

I need "%20" instead of "+" while encoding the string.

String query = 'Test Example';
Output should be: Test%20Example

Please help me out.

Thanks in advance.
 
Łukasz JanickiŁukasz Janicki
Hi Madhav,
Have you found solution for this?
Madhav GoyalMadhav Goyal
Hi,

I am using the below code for this.
String query = EncodingUtil.urlEncode('Test Example', 'UTF-8').replace('+', '%20') ;

 
Ganesh RajputGanesh Rajput
Hello @Dev TNOPPV5 
Have you got the solution I have the same requirement
please replay how you solved this?
Maneesh Kumar 3Maneesh Kumar 3
Hi Madhav ,
There is no need to use  replace('+', '%20') .
You can only use :
String query = EncodingUtil.urlEncode('Test Example', 'UTF-8'); 
it will work as expected in URL no need to replace '+' with '%20'.