-2

Need help I am trying to connect to XRAY JIRA using Rest API and want to execute a case but getting 400 error response at step inputStream=new InputStreamReader(con.getInputStream(),"UTF-8")

java.io.IOException: Server returned HTTP response code: 400 for URL:

My Code is below:

    *HttpURLConnection con=null;
    InputStreamReader inputStream=null;
        URL jira_API_URL=new URL("https://jira.abc.com/rest/raven/latest/import/execution");
        
        
          String encodeCredentials=Base64.getEncoder().encodeToString(
          "kkris:testjira@234".getBytes("UTF-8"));
          con=(HttpURLConnection)jira_API_URL.openConnection();
          con.setRequestMethod("POST"); con.setDoOutput(true);
          con.setRequestProperty("Autherization", "Basic "+encodeCredentials);
          con.setRequestProperty("Content-Type", "application/json");
          con.setRequestProperty("X-Atlassian-Token", "nocheck");
         
                 

        try(OutputStream os=con.getOutputStream()){
            byte[] input=json.toString().getBytes("UTF-8");
            os.write(input,0,input.length);
        }
        inputStream=new InputStreamReader(con.getInputStream(),"UTF-8");*

Note :Would like to add that I am able to hit this RestAPI using postman and Restassured & able to execute testcase in XRAYJIRA successfully

1 Answers1

0

Well, first of all we need to clarify if you have Xray on Jira server/datacenter or Xray on Jira cloud, as they are different products and the APIs are also slightly different. From your example, it seems that you're targetting Xray on Jira server/datacenter, and that you aim to import results using the Xray JSON format and respective endpoint as detailed here. The endpoint URL in that case should either be <jira_base_url>/rest/raven/1.0/import/execution or <jira_base_url>/rest/raven/2.0/import/execution

Also, please make sure the Xray JSON content you submit follows this syntax.

Note: you may want to have a look at this repo which contains some sample code for submiting results in Java and other languages, including a proof-of-concept client api.

The response body content may show you clues about what's wrong. You can start by using curl utility first as shown here and then implement the java code.

curl -H "Content-Type: application/json" -X POST -u admin:admin --data @data.json http://yourserver/rest/raven/1.0/import/execution
Sérgio
  • 1,777
  • 2
  • 10
  • 12
  • Thanks for your inputs, I was using wrong spelling of authorization so so corrected it . but I am getting 400 now. I want to add that I am able to hit JIRA XRAY API successfully using Postman and RestAssured too. Also, 1.0,latest both works fine when i tried using postpman .Need help in above code – Devkant Krishnatrey Mar 07 '22 at 04:04
  • Updated more details and deleted print command. Now error moves to input stream – Devkant Krishnatrey Mar 07 '22 at 06:09