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

GET details from JIRA using REST API

Ganesh September 27, 2016

Hi everyone..!!

 

I am trying to get details of an Issue using REST API posting data to REST url directly. Please find my code below.

 

URL jiraURL = new URL("http://localhost:8080/jira/rest/api/2/issue/TEST-12");
HttpURLConnection connection = (HttpURLConnection)jiraURL.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
connection.setDoInput(true);
String userCredentials = "jiraapi:jiraapipwd";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
connection.setRequestProperty ("Authorization", basicAuth);
connection.connect();
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream(),StandardCharsets.UTF_8);
wr.write(data.toString());
wr.flush();
wr.close();
Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),StandardCharsets.UTF_8));
for (int c; (c = in.read()) >= 0; System.out.print((char)c));

 

I get the following error when I run this code as a Java application in eclipse. I didn't use Maven. I have the required jars added to Build path manually.:

Server returned HTTP response code: 405 for URL

 

Is this the right method or do I have to follow some other method to communicate with JIRA. Please help..!!

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
noamdah
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.
September 27, 2016
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * Created by Noam.Dahan on 27/09/2016.
 */
public class Main {
    public static void main(String[] args) {
        URL jiraURL;
        try {
            jiraURL= new URL("http://localhost:8080/rest/api/2/issue/TEST-12") ;
            HttpURLConnection connection = (HttpURLConnection)jiraURL.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Accept", "*/*");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            String userCredentials = "user:pass";
            String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
            connection.setRequestProperty ("Authorization", basicAuth);
            connection.connect();
//            OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream(),StandardCharsets.UTF_8);
//            wr.write(data.toString());
//            wr.flush();
//            wr.close();
            Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
            for (int c; (c = in.read()) >= 0; System.out.print((char)c));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Ganesh September 27, 2016

Thanks Noam...!!smile

TAGS
AUG Leaders

Atlassian Community Events