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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

2 votes
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 # 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. 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Data Center

Introducing Data Center Community licenses

I'm Alison Huselid, Head of Product for Data Center at Atlassian. As we shared in our last post, we’ve been working on a solution for those of you who work for charitable non-profit organizations tha...

696 views 10 39
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