Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get expand=changelog by API calls from JIRA server via Java

Kaan Emek October 19, 2022

Hi everyone, 

We are trying to save all the changelog (String) via expand of all issues. Where we itterate trough all issues in Java.

We are using JIRA sever. In the browser I can open the page:
'.../rest/api/issue/{key}?expand=changelog'

The main goal of this approach is, to get the transitions of the different status exp. 

"created": "2022-05-11T08:14:34.000+0200",
"items": [
{
"field": "status",
"fieldtype": "jira",
"from": "1",
"fromString": "Open",
"to": "3",
"toString": "In Progress"
}
]
 

 If i try it with this code:

public static String urlReader(String theUrl) // .../rest/api/issue/{key}?expand=changelog
{
StringBuilder content = new StringBuilder();
// Use try and catch to avoid the exceptions
try
{
URL url = new URL(theUrl); // creating a url object
URLConnection urlConnection = url.openConnection(); // creating a urlconnection object

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;

while ((line = bufferedReader.readLine()) != null)
{
content.append(line + "\n");
}
bufferedReader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println(content.toString());
return content.toString();
}

Im getting this error: Server returned HTTP response code: 401 for URL:


Do someone know how to solve this problem?

Thanks

1 answer

0 votes
Radek Dostál
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.
October 19, 2022

Well, 401 means Unauthorized (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401), so your problem is not in the code, but rather that the HTTP request you are sending does not contain any credentials.

You need to set an 'Authorization: <value>' header on the request. Ideally a PAT: https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html

 

I don't know if you can set headers on URLConnection, I don't use this and never have, but here's one example of authorization header with HttpClient which I use in my scripts:

import com.atlassian.jira.component.ComponentAccessor
import org.apache.http.HttpEntity
import org.apache.http.HttpHeaders
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.util.EntityUtils

//either can use this for dynamical base url, or just hardcode it
final String BASE_URL = ComponentAccessor.getApplicationProperties().getString("jira.baseurl")

//this is our http client we will use to execute http requests
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build()

//HttpGet, HttpPost, etc., whatever it is we are doing
HttpGet httpGet = new HttpGet(BASE_URL + "/whatever/rest/endpoint/I/want")

//Set the authorization header
//If you don't have PAT, you can do "Basic user:pwd" but it's a really bad practice, use PAT
//Alternatively you can also provide a JSESSIONID cookie instead of a header
httpGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer <personal access token>")

//Execute the http request via our client
CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpGet)

//Do something with the response
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode()
HttpEntity entity = closeableHttpResponse.getEntity()
String responseString = entity != null ? EntityUtils.toString(entity) : null

//Close the response
closeableHttpResponse.close()

//Finally close the http client when we're done
closeableHttpClient.close()
Kaan Emek October 21, 2022

Thank you for your help. I cant test it. i cant import 

import com.atlassian.jira.component.ComponentAccessor

do you have any idea how i can import it? maybe a maven dependency ?

thanks

Radek Dostál
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.
October 21, 2022

Oh, you're actually using java outside Jira, not groovy, interesting.

So then just remove the component accessor. The only thing it does in my example is getting Jira's baseurl.

Like Kaan Emek likes this

Suggest an answer

Log in or Sign up to answer