Forums

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

Fetching a field from issues in JIRA to external program.

ODesh February 6, 2018

I want to fetch data of a field in issues from JIRA to an external Java application. Below is the simple java code written

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class JiraIssueDescription {

public static void main(String[] args) {

  try {

    URL url = new URL("https://****.atlassian.net/rest/agile/1.0/issue/41459");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept", "application/json");
    conn.setRequestProperty("username", "***@abc.com");
    conn.setRequestProperty("password", "****");

    if (conn.getResponseCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(
        (conn.getInputStream())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    conn.disconnect();

  } catch (MalformedURLException e) {

    e.printStackTrace();

  } catch (IOException e) {

    e.printStackTrace();

  }

}

}

But when I run the code I get connection errors.

java.net.UnknownHostException: ****.atlassian.net
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)

 Can anyone please help me with the errors. Do I have to implement authentication for rest api requests?

2 answers

1 accepted

0 votes
Answer accepted
ODesh February 13, 2018

Hi everyone, the issue got resolved by the below java code. Hope it helps. It uses Basic Http authentication.

 


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import org.junit.Test;

public class HowToReadFromAnURL {
    @Test
    public void readFromUrl() {
        try (InputStream in = getInputStreamFromUrl("https://jira.atlassian.com/rest/api/2/issue/JSWCLOUD-11658")) {
            System.out.println(convertInputStreamToString(in));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test(expected = RuntimeException.class)
    public void readFromUrlWithBasicAuth() {
        String user="aUser";
        String passwd="aPasswd";
        try (InputStream in = getInputStreamFromUrl("https://jira.atlassian.com/rest/api/2/issue/JSWCLOUD-11658",user,passwd)) {
            System.out.println(convertInputStreamToString(in));
        } catch (Exception e) {
            System.out.println("If basic auth is provided, it should be correct: "+e.getMessage());
            throw new RuntimeException(e);
        }
    }

    private InputStream getInputStreamFromUrl(String urlString,String user, String passwd) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");
        String encoded = Base64.getEncoder().encodeToString((user+":"+passwd).getBytes(StandardCharsets.UTF_8));  
        conn.setRequestProperty("Authorization", "Basic "+encoded);
        return conn.getInputStream();
    }

    private InputStream getInputStreamFromUrl(String urlString) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");
        return conn.getInputStream();
    }

    private String convertInputStreamToString(InputStream inputStream) throws IOException {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }
        return result.toString("UTF-8");
    }
}
0 votes
Fabio Racobaldo _Catworkx_
Community Champion
February 7, 2018

Hi,

probably the URL is incorrect. The correct URL for your rest is 

("https://****.atlassian.net/rest/api/2/issue/41459");

for more information read here (https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-issue-issueIdOrKey-get).

About authentication you caread here https://developer.atlassian.com/cloud/jira/platform/rest/#authentication

Regards,

Fabio

ODesh February 7, 2018

Hi Fabio, I have the below URL with Postman and I'm able to get the response.

 ("https://****.atlassian.net/rest/agile/1.0/issue/41459");

 Also, I've tried with the URL suggested by you, but its giving the same error.

P.S. Thanks for the suggestion

Suggest an answer

Log in or Sign up to answer