Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,295,747
Community Members
 
Community Events
165
Community Groups

HTTP post request using groovy script

Hi All,

I tried a groovy script for httpbuilder and when running it in script runner i am getting result as "null".

could you guys please help me out this, where i went wrong with the script? Here I attach my script code.

Thanks 

 

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
def http = new HTTPBuilder('https://abc.googleapis.com/v1/projects/$project/topics/$topic:publish')
def project = 'app-ne-preprod-lls-atlassian'
def topic = 'xyz_project_all_issue_events'
def access_token = 'ya29.c.KmC6B7IHIUh8afwaYc5KNSwlEtiyTVXLvcRDjd-t6WJFESYPlV4kxmDB9vAhO9snExsPJVYzd2SXIl5uBQMVlDRwsCl1ORpdDD5Pt697tGdA5rxrYZpAR1Yhz6CC9FMAhbY'
http.request(POST) {
//uri.path = 'https://abc.googleapis.com/v1/projects/$project/topics/$topic:publish'
requestContentType = ContentType.JSON
headers.'Authorization' = "token $access_token"
body = ["messages": ["data": "test"]]
response.success = { resp ->
println "Success! ${resp.status}"
}
response.failure = { resp ->
println "Request failed with status ${resp.status}"
}
}

 

1 answer

0 votes

Hi @Sekhar Chandra 

I see potentially 2 errors in 

def http = new HTTPBuilder('https://abc.googleapis.com/v1/projects/$project/topics/$topic:publish')

1) $variable are not expanded in strings with single quote.

2) $project and $topic are not defined yet when you initialize the http object

This it like this:

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*

def project = 'app-ne-preprod-lls-atlassian'
def topic = 'xyz_project_all_issue_events'
def access_token = 'ya29.c.KmC6B7IHIUh8afwaYc5KNSwlEtiyTVXLvcRDjd-t6WJFESYPlV4kxmDB9vAhO9snExsPJVYzd2SXIl5uBQMVlDRwsCl1ORpdDD5Pt697tGdA5rxrYZpAR1Yhz6CC9FMAhbY'

def http = new HTTPBuilder("https://abc.googleapis.com/v1/projects/$project/topics/$topic:publish")

http.request(POST) {
//uri.path = 'https://abc.googleapis.com/v1/projects/$project/topics/$topic:publish'
requestContentType = ContentType.JSON
headers.'Authorization' = "token $access_token"
body = [messages: [data: "test"]]
response.success = { resp ->
println "Success! ${resp.status}"
}
response.failure = { resp ->
println "Request failed with status ${resp.status}"
}
}

Thanks @Peter-Dave Sheehan 

when running this code in script runner I'm getting "connection reseterror like this,

 

java.net.SocketException: Connection reset at org.apache.http.conn.ssl.SSLSocketFactory.createLayeredSocket(SSLSocketFactory.java:573) at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:557) at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:414) 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$0.call(Unknown Source) at Script226.run(Script226.groovy:12)

That might be something specific to your environment.

You should make sure you are able to make a simple (like just GET without authorization) request and work it out from there.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

308 views 0 9
Join discussion

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