Monday, March 23, 2020

System.CalloutException: Read timed out error in Salesforce

When you are sending the request to other systems sometimes you may face the fatal error like
System.CalloutException: Read timed out error

To resolve the issue increase the timeout limit,
the minimum timeout limit is  1 millisecond
the maximum timeout limit is 120 seconds (2minutes).

The request.setTimeout method accepts the parameter is an integer in milliseconds so pass the value in milliseconds

Ex: 20 sec * 1000  ms / sec = 20,000 ms

Sample Code

HttpRequest httpRequest = new HttpRequest();    
httpRequest.setHeader('Authorization', 'OAuth ' + getSessionId());        
httpRequest.setHeader('Authorization', 'Bearer ' + getSessionId()); 
httpRequest.setHeader('Content-Type', 'application/json');
httpRequest.setHeader('Accept', 'application/json');

Http http = new Http();
HTTPResponse res = http.send(httpRequest);
String strResponse = res.getBody();