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..!!
Community moderators have prevented the ability to post new answers.
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(); } } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.