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
Brett Phillips 9Brett Phillips 9 

Repeated Read timed out on HttpRequest

I have a puzzling issue that I cannot figure out. I have a very simple REST service running on an external server returning a small JSON response for every GET request. It is set up as a simple service to verify I can connect to it from Salesforce. This service returns without issue when hit from a browser, CURL, etc. However, when we try from Salesforce, it takes a number of tries to get a return. Here is the simple Apex code I've wrote the check the service:
 
Http h = new Http();
HttpRequest req = new HttpRequest();
String url = '<webservice being called>';
req.setEndpoint(url);
req.setMethod('GET');
HttpResponse res;
while(res == null){   
	try{
		res = h.send(req);
		System.debug(res.getBody());  
	} catch(CalloutException e) {
		System.debug(e.getMessage());
	}
}

When running this section of code, there are a number of times "Read timed out" happens when making the HttpResponse call before a successful response is received. Here are a couple examples of an output from the logs:

User-added image

Success request after 1 attempt

As you can see, there is no reason or frequency for the timeouts. I have control over the server that is accepting the requests but unfortuantely I do not have control over the network level firewall. I have been told there are no restrictions to the network for any and all traffic. That seems to be the case because calls can be made from everywhere with issues. On the server logs, I see a successful request coming in but do not see any requests when there is a timeout.

Is there anything from the Salesforce side that could be causing this issue? I want to make sure I cover all possible points in this process that could be causing this failure. 
matt.ian.thomasmatt.ian.thomas
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_httprequest.htm

Check out the setTimeout method. Note that the default timeout is 10 seconds, as detailed here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_timeouts.htm

I would also remove your callout from that while loop; as with DML and queries, it's not a good practice to make an HttpRequest inside of a loop.
Brett Phillips 9Brett Phillips 9
Thanks Matt. 

I have used the setTimeout method. I've tried 60 seconds, 15 seconds, 2 seconds, etc (all calculated to milliseconds for the setTimeout method) and get the same results, to remove that as an issue.

Oh I completely agree - this is terrible practice but I'm just using this to debug this and make sure Salesforce can actually connect to this service at some point. Once I can establish a reliable, stable connection, I will use good practices :)
matt.ian.thomasmatt.ian.thomas
Interesting. I presume that if a request is timing out that the service is the cause, not Salesforce. However, it sounds like sending a request outside of Salesforce works just fine so that would indicate otherwise. I'm honestly not sure, since I haven't had this happen to me before. Best of luck and post back here if you find any new clues!
kvorkvor
Hello Brett,

We face same issue. First calls usually fail fast with read timeout.
Have you managed to address this issue?

Even if we set timeout to 15-20seconds it fails much faster.
We have tried calls with postman/curl and we get response after 2-3 seconds.
It seems that setTimeout is ignored (by the way there is no way to check set value since there is no getter).

Best

Kostas