JIRA API returns 405 errors. Works fine with curl

Nathan Reddy December 2, 2021

I am trying to make a simple Jira REST API call from my Eclipse  to Jira Cloud. This works fine with curl on same machine using the same URL and same credentials. 

 

curl -D- -X GET -H "Authorization:Basic bmF0aGFuQHMODIFIEDBYME==" -H "Content-type:application/json" "https://tracecloud.atlassian.net/rest/api/2/issue/PID-7"

 

Response : 


HTTP/1.1 200 OK
Server: AtlassianProxy/1.19.3.1
vary: Accept-Encoding

 

I am trying the same thing in my Java code : 

 

    

     try {

             // Create URL object

                   

            URL url = new URL("https://tracecloud.atlassian.net/rest/api/latest/issue/PID-7");

             conn = (HttpURLConnection) url.openConnection();

             conn.setDoOutput(true);

             conn.setRequestMethod("POST");

             conn.setRequestProperty("Content-Type", "application/json");

             

             conn.setRequestProperty("Authorization", "Basic bmF0aGFBase64JiraAPItoken==");

          

             os = conn.getOutputStream();

             os.write(input.getBytes());

             os.flush();

             

             System.out.println("response code is " + conn.getResponseCode());

             if(conn.getResponseCode() == 200){

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

                 while((output = br.readLine()) != null){

                     loginResponse += output;

                 }

                 conn.disconnect();

             }

         } catch (Exception ex) {

             System.out.println("Error in loginToJira: " + ex.getMessage());

             loginResponse = "ERROR";

         }

     

     System.out.println("loginResponse  is " +loginResponse);

     

I did some research and tried the following :

 

1. Used HTTPS

2. This URL works fine, but returns HTML web page and not JSON.      // URL url = new URL( "https://tracecloud.atlassian.net/jira/software/projects/PID/boards/1?selectedIssue=PID-1");

3.  conn.setRequestProperty("dataType", "json");

 4. conn.setRequestProperty("cache-control", "no-cache");

  5.  conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

 

I used to get 403 error, now I get 405 . Any help is much appreciated. 

 

          

1 answer

0 votes
Charlie Misonne
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 3, 2021

Hi Nathan,

In the java code you're performing a POST method, in curl you use a GET method.

That's most likely your issue.

Nathan Reddy December 3, 2021

Hi Charlie,

 

Thanks for the response. I had tried it with GET too. Same issue (405) error

 

             conn = (HttpURLConnection) url.openConnection();

             

             // Set properties

             conn.setDoOutput(true);

             conn.setRequestMethod("GET");

             conn.setRequestProperty("Content-Type", "application/json");

            // conn.setRequestProperty("dataType", "json");

             

             // this encoding is for nathan@tracecloud.com:JIRAAITOKEN

             conn.setRequestProperty("Authorization", "Basic bmF0aGFuQHRyYW

 

....

 

Nathan

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events