About “Unable to tunnel through proxy. Proxy returns HTTP/1.1 503 Service Unavailable” Error

By | August 29, 2021

Error Message

System.CalloutException: Unable to tunnel through proxy. Proxy returns “HTTP/1.1 503 Service Unavailable”

Repro

To call the REST API of Salesforce, execute the following code.

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:NamedCredentialApiName/some_path');
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());

How to fix this error

Solution1(If you are trying to call the API of an external system)

Add your salesorce instance IP address to the whitelist of external systems.

This means, in effect, allowing all possible IP addresses in the region you belong to.

See this help article for details.

※If the 503 error is caused by the access load of an external system, the problem is not on the Salesforce side.

Solution2( If you are trying to call the API of Salesforce)

Correct the Named Credentials` URL FIELD VALUE to the correct one.

Run the following method in the developer console to check the return value and use the result, it will probably work.

String.valueOf(Url.getOrgDomainUrl().toExternalForm());