REST API | connect timeout error | Request not sent to uri= URL

Neeraj Modi
Tera Guru

Hi All, 

 

I'm trying to hit the endpoints using below script.

  1. It is working fine in POSTMAN tool only when connected to VPN, if not then it won't work in POSTMAN as well.
  2. When I tried to execute the below script in the background script it is working fine for other endpoints only for this specific endpoints it is not working and throwing error which is shown below.
  3. I have already tried this solution but it didn't work for me.
getToken('GET', '/api/v1/services');

function getToken(method, endpoint) {
    try {
        var finalEndpoint = "https://endpoint" + endpoint;

        var request = new sn_ws.RESTMessageV2();
        request.setHttpMethod(method);
        request.setEndpoint(finalEndpoint);
        request.setRequestHeader("Accept", "application/json");
        request.setRequestHeader("Content-Type", "application/json");
        request.setRequestHeader("Authorization", "Bearer token");
               
        var response = request.execute();
        var responseBody = response.getBody();
        var httpResponseStatus = response.getStatusCode();
		
        gs.info('modi@@@--- httpResponseStatus ' + httpResponseStatus);
		gs.info('modi@@@--- responseBody ' + responseBody);
        
		gs.info('modi@@@--- haveError ' + JSON.stringify(response.haveError()));
		gs.info('modi@@@--- getErrorCode ' + JSON.stringify(response.getErrorCode()));
        gs.info('modi@@@--- getErrorMessage ' + JSON.stringify(response.getErrorMessage()));
        		
    } catch (ex) {
        var message = JSON.stringify(ex);
		gs.info('modi@@@--- message ' + message);
        gs.info(message);
        
    }
}

Error message what I'm getting-  

*** Script: modi@@@--- httpResponseStatus 0
*** Script: modi@@@--- responseBody 
*** Script: modi@@@--- haveError true
*** Script: modi@@@--- getErrorCode 1
*** Script: modi@@@--- getErrorMessage "Request not sent to uri= "endpoint" : java.net.SocketTimeoutException: connect timed out"

 

 

1 REPLY 1

Ratnakar7
Mega Sage
Mega Sage

Hi @Neeraj Modi ,

 

The error message "Request not sent to uri= "endpoint" : java.net.SocketTimeoutException: connect timed out" indicates that there is a connection timeout issue when trying to establish a connection to the specified endpoint.

Here are a few things you can check and try to resolve the issue:

  1. Verify the Endpoint: Double-check that the endpoint URL you are using is correct and accessible. Make sure there are no typos or missing characters in the URL.

  2. Test the Endpoint outside of ServiceNow: Try accessing the endpoint using a tool like cURL or a web browser to ensure that the endpoint is accessible and returns the expected response. This will help determine if the issue is specific to ServiceNow or if it is a network-related problem.

  3. Check Network Connectivity: Ensure that the ServiceNow instance has network connectivity to reach the endpoint. Check any network configurations, firewalls, or proxy settings that may be blocking the connection.

  4. Increase Connection Timeout: If the endpoint requires a longer connection establishment time, you can try increasing the connection timeout value in your ServiceNow script. You can modify the following line to set a longer timeout value:

 

request.setHttpTimeout(timeoutInSeconds);

 

     5. Replace timeoutInSeconds with the desired timeout value in seconds. For example, you can set it to 60 for a 60-second timeout.

     6. Check VPN Connection: Since you mentioned that the script works in POSTMAN when connected to a VPN, make sure that the ServiceNow instance is also connected to the same VPN when executing the script. Verify the VPN connectivity on the ServiceNow server.

    7. Check ServiceNow Outbound Requests: Ensure that any outbound request restrictions or security policies in ServiceNow are not blocking the communication to the endpoint. You can check the Outbound Requests module in ServiceNow to see if there are any rules that may be affecting the request.

    8. Debugging and Logging: Enable additional logging and debugging in your ServiceNow script to get more information about the issue. You can use gs.debug() or gs.error() statements to log specific details during the execution of the script.

 

If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.

 

Thank you!

Ratnakar