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
shazzshazz 

( System.CalloutException: Read timed out ) on Callout to an external RPC based API using HTTP

Hi

 

I m trying to callout an external API using the following code :

 

public class Testapicall {

 
  public String resultProp {get;set;}                                  //Prop for result
  public String inputProp {get;set;}                                   //Prop for input

 
  public PageReference submit() {                                      // On submit button click
         resultProp = getResult(inputProp);                            // Call function getResult that makes HTTP POST call to internal API
         return null;
      }
     
 
    
  private String getResult(String inputProp) {                         // HTTP POST and results back
        
     String json;                                                      // for storing back the json response of the HTTP call
     
     HttpRequest req = new HttpRequest();                              // POST method calling
     Http http = new Http();
     req.setMethod('POST');       
    
     req.setTimeout(2000);          
    
     String username = 'abc';
     String password = 'def';                                     // Settting up of username and password
     String contentTypes = 'application/json';
     String accepting = 'application/json';
    
    
      
     Blob headerValue = Blob.valueOf(username + ':' + password);                          //Setting header values
     String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
     req.setHeader('Authorization', authorizationHeader);
      
        
     req.setHeader('Content-Type', contentTypes);
     req.setHeader('Accept', accepting);
    
     String inp= '{"version": "1.1","method": "sforce_ping","params":{"name":"HELLO"}}';
     req.setBody(inp);                                                                                                                            
    
     String url = 'http://abcd.com:3100/services';         // Setting up end point
     req.setEndpoint(url);
     HTTPResponse resp = http.send(req);                               // Getting back the response
    
          System.debug(resp.toString());
          System.debug('STATUS:'+resp.getStatus());
          System.debug('STATUS_CODE:'+resp.getStatusCode());
    
     json = resp.getBody().replace('\n','');                           // Storing json in the 'json' string
     json = json.replace('\r','');
        
    try{
     String json1=json.replace('\t','');                                // Removing whitespace from json and storing it in json1
    
           System.debug('reading response as json1--->>>>' + json1 );         // ((((((((DEBUG POINT))))))))))
    
     JSONObject j = new JSONObject(json1);                              // JSONize
    
           System.debug('reading response as json object j --->>>>' + j );   // ((((((((DEBUG POINT))))))))))
         
     return toFinalResult(j).toDisplayString();                         // Calling Function toFinalResult which fetches the response from the JSONized response
     }
    catch (JSONObject.JSONException e) {                                // Exception handling
     return 'Error parsing JSON response: '+e;
     }
   
  }
 
 
  private FinalResult toFinalResult(JSONObject resp) {                  // Function toFinalResult which fetches the response from the JSONized response
     FinalResult finalres = new FinalResult();
     try {
          finalres.version = resp.getString('version');                    // Fetching the version
          finalres.result = resp.getString('result'); 
     }
     catch (Exception e) {
     //fail
     }
     return finalres;
  }
    
  private class FinalResult {                                            // Class to display final result
         public String version;
         public String result;
         public String toDisplayString() {
         return result;
        
         }
      }
     
  }

 

The curl request that works fine is :

curl -X POST -H "Content-Type: application/json" -H "Accept:application/json" -u abc:def -d '{"version": "1.1","method": "sforce_ping","params":{"name"

The curl response is :
{"version": "1.1", "result": "\"HELLO\""}
Can some one guide me where I m going wrong as it always calls that exception of timeout. Is it the Authentication issue?



SrikanthKuruvaSrikanthKuruva

did you try to access the webservice from your browser?

or you can try using the soap ui tool to check the response.

sdetweilsdetweil

well, I bet     
     String url = 'http://abcd.com:3100/services';         // Setting up end point

 

is behind your firewall, the curl works from YOUR system INSIDE the firewall..

 

salesforce is outside the firewall..

 

I could not connect to abcd.com from my system.

SrikanthKuruvaSrikanthKuruva

check if you are getting the proper response using the soap UI tool.

sdetweilsdetweil

SoapUI will run inside his firewall and look good

 

 

SrikanthKuruvaSrikanthKuruva

did you whitelist the salesforce ip ranges that can access your webservice.

check the following link

http://boards.developerforce.com/t5/Apex-Code-Development/System-CalloutException-IO-Exception-Read-timed-out/m-p/283337/highlight/true#M49479