Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,456,314
Community Members
 
Community Events
176
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

Atlassian Community Events