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)
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
what does it mean fields=""? Try to skip it. make your url like this
/rest/api/2/issue/SD-233373?expand=changelog
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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=""
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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: '""'])
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You code works fine. Make sure you enter the right user and the user password. And you use basic authentication in your Jira instance.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do you try Postman on the Jira instance pc? I mean you should try it on the same pc, where Jira is installed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I do not think it could be a problem. But try to fix it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I guess, so. The problem is with cert
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Then it worked fine for me
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.