Encoding URL in Tooling API call
Encoding URL in Tooling API:
When using Tooling API to fetch the metadata for a particular component, we add query in the endpoint URL many times. Sometime the parameters we add has a space in between. If you insert a keyword with a space in the endpoint URL, calling the Tooling API through Rest will throw below error:
Tooling API Call:
String query = '/services/data/v40.0/tooling/query/?q=Select+id,Name,Metadata+from+Profile+where+name+=\''+profileName+'\'';
req.setEndpoint(baseURL + query);
The profileName variable hold 'System Administrator'
ERROR:
13:16:25:020 CALLOUT_RESPONSE [75]|System.HttpResponse[Status=Unknown Version, StatusCode=400]
13:16:25:021 USER_DEBUG [77]|DEBUG|response<h1>Bad Message 400</h1><pre>reason: Unknown Version</pre>
To resolve this error make sure to encode the endpoint URL with method urlEncode() of EncodingUtil class.
Something as below:
String query = '/services/data/v40.0/tooling/query/?q=Select+id,Name,Metadata+from+Profile+where+name+=\''+EncodingUtil.urlEncode(profileName, 'UTF-8')+'\'';
req.setEndpoint(baseURL + query);
Hope this helps.
Great sir ..thanks :)
ReplyDeletethanks a lot!
ReplyDeletegümüşhane
ReplyDeletebilecik
erzincan
nevşehir
niğde
UZAUB1