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.HTTPBuilderimport static groovyx.net.http.ContentType.*import groovyx.net.http.ContentTypeimport 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.JSONheaders.'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}"}}
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.HTTPBuilderimport static groovyx.net.http.ContentType.*import groovyx.net.http.ContentTypeimport 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 @PD Sheehan
when running this code in script runner I'm getting "connection reset" error 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.
It looks like you're new here. Sign in or register to get started.