Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

<Get> request to the issue from Scriptrunner functions

Saida March 26, 2018

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.
March 30, 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: '""'])
0 votes
Saida March 29, 2018

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.
March 29, 2018

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

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

Saida March 29, 2018

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.
March 29, 2018

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
Saida March 30, 2018

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.
March 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.

Saida March 27, 2018

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.
March 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.

Saida March 27, 2018

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.
March 27, 2018

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

Saida March 27, 2018

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.
March 27, 2018

I guess, so. The problem is with cert

Saida March 29, 2018

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.
March 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?

Saida March 29, 2018

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.
March 29, 2018

Then it worked fine for me

TAGS
AUG Leaders

Atlassian Community Events