System.CalloutException: Read timed out

WHEN IT OCCURS

It takes place in HTTP callout made from Apex.

If you’re getting a Read Time Out, it means the service is not returning data in a timely manner. There’s nothing you can do on this end specifically; you need to contact the provider of this API. You may need to increase the selectivity of your search, optimize the search on the far end (e.g. add an index), or something else. You can’t extend the timeout beyond two minutes, so it needs to respond within that time.

HOW TO FIX

Salesforce has a governor limit which is enforced when you are sending the request to other systems. If the limit is reached, it shows a Read timed out error. The default timeout is 10 seconds. The maximum is 120 seconds (2 minutes) that can be set as below:

HttpRequest req = new HttpRequest();

req.setEndpoint(api_endpoint);

req.setMethod('POST');

req.setHeader(methodName, encodedData);

req.setBody(encodedData);

Http http = new Http();

req.setTimeout(120000);//max timeout is 120 seconds

HTTPResponse res = http.send(req);

String nvp=res.getBody();