Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Connection refused and Connection timed out exception while sending a post request to the rest api

Jamshaid
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 30, 2020

I am trying to send a post request for login to my Rest API which should have a username and password in the body. I am trying to run the same request on POSTMAN and it works pretty fine whereas when I run the script on SccriptRunner with the same credentials and the URL it Says (Connection refused) and the whole log is as below

java.net.ConnectException: Connection refused (Connection refused)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:326)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:221)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:165)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:434)
at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:366)
at groovyx.net.http.HTTPBuilder$request.call(Unknown Source)
at Script1264.run(Script1264.groovy:9)

 

Here is my Script

if I try the IP address in the script, it says Connection timed out and if I use link address, it says connection refused. What U might be doing wrong?

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovy.json.JsonSlurper
import net.sf.json.groovy.JsonSlurper

def http = new HTTPBuilder('http://address-to-api:port/login')
http.request(POST) {
requestContentType = ContentType.JSON
body = [username: 'username', password: 'password']

response.success = { resp, JSON ->

return JSON

}

response.failure = { resp ->
return "Request failed with status ${resp.status}"

}
}

 

 

I am putting the exact same link and credentials in the script which are working on POSTMAN and curl but it still says the connection refused and timed out. What might the error be?

thanks

 

1 answer

1 accepted

0 votes
Answer accepted
Jamshaid
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 1, 2020

I was having an issue with the server. otherwise, the code is working fine.

Admin Jira
Contributor
January 14, 2022

Hi Jamshaid,

 

You can tell me what exactly the problem was in your server 

 

Thanks 

Like Jamshaid likes this
Jamshaid
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 20, 2022

@Admin Jirasorry. I have been away. my server was not allowing the connections to the specified port as I remember.

Like Admin Jira likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events