Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

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

All you wanted to know about customer support KPIs

When working in customer support, it’s crucial to calculate, analyze and monitor specific numbers, called KPIs (Key Performance Indicators). They go hand-in-hand with customer satisfaction, problem d...

132 views 1 5
Read article

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