Call REST API from Java

I am trying to call JIRA REST API from Java function and I am not really sure how to do it.

Currently, I am using something like

String baseURL = ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL);
String address = baseURL + "/rest/api/2/issue/" + issueID;
URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");

which fails on authorization (401). I have found some suggestions to include encoded login and password to the request, but I would like to use the current logged user.

I found some tutorials for JIRA REST Java Client, but cant seem to get it working.

Can someone point me out to some simple example of calling internal REST API using logged users credentials? If such thing is even possible.

1 answer

0 votes

I believe you can do that through authenticated Cookies. You will need to use credentials to create those, but then you can make calls pointing to that cookie, which should validate the session.

This page is really helpful:

https://developer.atlassian.com/cloud/jira/platform/jira-rest-api-cookie-based-authentication/

Doesnt that just transfer the problem somewhere else?

I am trying to call the REST API from inside the Workflow Postfunction, I dont have access to the credentials. Sure, on my machine I can hardcode "admin"/"admin", but how would that work when you install the plugin on any other system?

I mean, I have been using Ajax REST calls without specifying any authentication, I assumed there would be some Java equivalent.

@Ivana Navratilova Were you able to find a solution to this? I have the same issue.

@Mathieu YargeauI dont remember exactly, but I think you can access most of the data from internal APIs directly through some accessors (like pulling out an issue or list of custom fields).

For most cases, yes, but I am trying to create an entity link (link between a Jira project and a Confluence space) and the only way I found was through a private REST API.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,790 views 18 22
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you