Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Edited

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

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

Hi Jamshaid,

 

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

 

Thanks 

Like Jamshaid likes this

@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

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you