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

<Get> request to the issue from Scriptrunner functions

Good day, 

I need to write a GET request to JIRA from a listener / a post-function. 

I have tried some code snippets found here, but they all dont work for me. Running from the Script Console, 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.apache.log4j.Logger;

def log = Logger.getLogger("com.onresolve.jira.groovy");

//String targetURL = "https://jiratest:8443/rest/api/2/issue/SD-233373?expand=changelog&fields=%22%22"; //?expand=changelog&fields=%22%22
String targetURL = 'https://jiratest:8443/rest/api/2/issue/SD-233373'
try {
URL restServiceURL = new URL(targetURL);
String username = "username"
String password = "password"
HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("Accept", "application/json");
String encoded = Base64.getEncoder().encodeToString((username+":"+password).getBytes(StandardCharsets.UTF_8));
httpConnection.setRequestProperty("Authorization", "Basic " + encoded);

if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("HTTP GET Request Failed with Error code : "
+ httpConnection.getResponseCode());
}

BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
(httpConnection.getInputStream())));

String output;
log.debug("Output: \n");

while ((output = responseBuffer.readLine()) != null) {
return output;
}

httpConnection.disconnect();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

The code returns me 

 

 

Error

HTTP GET Request Failed with Error code : 403

java.lang.RuntimeException: HTTP GET Request Failed with Error code : 403 at Script707.run(Script707.groovy:26)

 

3 answers

1 accepted

1 vote
Answer accepted

The script should be like this:

import groovyx.net.http.HTTPBuilder
import net.sf.json.JSONArray
import org.apache.http.HttpRequest;

import org.apache.http.protocol.HttpContext;
import org.apache.http.HttpRequestInterceptor;

def JIRA_API_URL = "https://yourjiradomain"

def jira = new HTTPBuilder(JIRA_API_URL);
jira.client.addRequestInterceptor(new HttpRequestInterceptor() {
void process(HttpRequest httpRequest, HttpContext httpContext) {
httpRequest.addHeader('Authorization', 'Basic ' + 'username:password'.bytes.encodeBase64().toString())
}
})

def resp = jira.get(path: '/rest/api/2/issue/MAV-2', query : [expand:'changelog', fields: '""'])
0 votes

You code works fine. Make sure you enter the right user and the user password. And you use basic authentication in your Jira instance.

Hi Alexey Matveev, 

I dont actually know why it is not working. 

The issue exists, the username and password are 100% correct, and there is basic authentication on the instance. The same request works fine from Postman, but from console it constantly returns 403 Forbidden. 

Do you try Postman on the Jira instance pc? I mean you should try it on the same pc, where Jira is installed.

No, I'm testing from my local pc. 

Can this be because of the Base URL problem (SSLHandshakeException Health Check: JIRA Base URL) ? I have not considered this problem since I get 403 error, not the certificate problem. 

I do not think it could be a problem. But try to fix it.

I have tested the request from the instance itself (curl, linux os). 

curl -D- -u username:password -X GET -H "Content-Type: application/json" https://jiratest:8443/rest/api/2/issue/SD-233373

Returns:

Peer's certificate issuer is not recognized. 

 But if add --insecure, 

curl -D- --insecure -u username:password -X GET -H "Content-Type: application/json" https://jiratest:8443/rest/api/2/issue/SD-233373

 It all works as expected. So does it mean that the root of the problem is cert? 

I guess, so. The problem is with cert

Hi Alexey, 

Our admins fixed the issue with a cert, and curl command works fine now. But still getting 403 running this script. 

Do you have an idea how this can be fixed? Does my script work for you? 

I tired this one

curl -D- -u username:password -X GET -H "Content-Type: application/json" https://jiratest:8443/rest/api/2/issue/SD-23337

it worked just fine. You have a Jira Server right? not Cloud?

Yes, we are on a server instance. 

Then it worked fine for me

Ok seems that I found another way to get the data, but it works only with the mentioned path, 

import groovyx.net.http.HTTPBuilder
import net.sf.json.JSONArray
import org.apache.http.HttpRequest;

import org.apache.http.protocol.HttpContext;
import org.apache.http.HttpRequestInterceptor;

def JIRA_API_URL = "https://jiratest:8443"

def jira = new HTTPBuilder(JIRA_API_URL);
jira.client.addRequestInterceptor(new HttpRequestInterceptor() {
void process(HttpRequest httpRequest, HttpContext httpContext) {
httpRequest.addHeader('Authorization', 'Basic ' + 'user:password'.bytes.encodeBase64().toString())
}
})

def resp = jira.get(path: '/rest/api/2/issue/SD-233373') //?expand=changelog&fields=""

return resp

I need to get the changelog fields, /rest/api/2/issue/SD-233373?expand=changelog&fields=""

 but when I update the request, it returns me 

2018-03-29 16:50:32,693 WARN [common.UserScriptEndpoint]: Script console script failed: 
groovyx.net.http.HttpResponseException:
at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:651)
at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:503)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:223)
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.get(HTTPBuilder.java:285)
at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:255)
at groovyx.net.http.HTTPBuilder$get.call(Unknown Source)
at Script26.run(Script26.groovy:18)

 @Alexey Matveev [cPrime] could you please help with this? 

what does it mean fields=""? Try to skip it. make your url like this

/rest/api/2/issue/SD-233373?expand=changelog

fields="" hides all information about fields, which is included to the response (found somewhere) and reduces the size of the data. 

But it doesnt work both for 

/rest/api/2/issue/SD-233373?expand=changelog

and 

/rest/api/2/issue/SD-233373?expand=changelog&fields=""

Try like this:

import groovyx.net.http.HTTPBuilder
import net.sf.json.JSONArray
import org.apache.http.HttpRequest;

import org.apache.http.protocol.HttpContext;
import org.apache.http.HttpRequestInterceptor;

def JIRA_API_URL = "https://jira-test.raiffeisen.ru"

def jira = new HTTPBuilder(JIRA_API_URL);
jira.client.addRequestInterceptor(new HttpRequestInterceptor() {
void process(HttpRequest httpRequest, HttpContext httpContext) {
httpRequest.addHeader('Authorization', 'Basic ' + 'username:password'.bytes.encodeBase64().toString())
}
})

def resp = jira.get(path: '/rest/api/2/issue/MAV-2', query : [expand:'changelog', fields: '""'])
Like Kevin Vanderbeken likes this

This is what I needed! Thank you!

If you will add this comment as a new answer, I will check it as a solution. 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Opsgenie

What’s New in Opsgenie October 2019

Hi there, we have a lot of exciting announcements this month, specifically around Dark Mode. You don’t want to miss out!    Dark Mode for Mobile App is here! A few weeks ago, we annou...

476 views 1 12
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