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

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

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 23, 2020

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)

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 23, 2020

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.

TAGS
AUG Leaders

Atlassian Community Events