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

<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

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Mar 30, 2018 • edited Oct 21, 2018

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: '""'])

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 could you please help with this? 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Mar 29, 2018

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=""

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Mar 29, 2018 • edited

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 # people like 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. 

0 votes
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Mar 26, 2018

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. 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Mar 27, 2018

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. 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Mar 27, 2018

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? 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Mar 27, 2018

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? 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Mar 29, 2018

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. 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Mar 29, 2018

Then it worked fine for me

TAGS
AUG Leaders

Atlassian Community Events