0

I'm trying to create JIRA defects through JSON files using REST API. The reason is to create large scale of defects rather than creating one by one through the JIRA UI.

The following is the code.

public class JiraBug {

@SuppressWarnings({ "unchecked", "rawtypes", "resource", "deprecation" })
public static String makeRequest(String path, JSONObject holder)
        throws Exception {

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpost = new HttpPost(path);

        StringEntity se = new StringEntity(holder.toString());
        httpost.setEntity(se);
        httpost.setHeader("Accept", "application/json");
        httpost.setHeader("Content-type", "application/json");

        ResponseHandler responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httpost, responseHandler);
        return response;
        }
public static void main(String[] args){
    try {
        JSONObject jsonobj = new JSONObject();
        File jsonFile = new File("JiraBug.json");
        if (jsonFile.exists()){
            InputStream is = new FileInputStream("JiraBug.json");
            String jsonTxt = IOUtils.toString(is, "UTF-8");
            jsonobj = new JSONObject(jsonTxt);
        }
        makeRequest("https://*<<Our_Company_JIRA_Server>>*/rest/api/2/issue",jsonobj);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

The following is the JSON Object.

{
"fields": {
   "project":
   { 
      "key": "TRAINING"
   },
   "summary": "Test Summary",
   "description": "Test Description",
   "issuetype": {
      "name": "Bug"
   },
    "priority": {
    "id":"2"
    }

} }

I'm getting a Bad request exception.

org.apache.http.client.HttpResponseException: Bad Request
at org.apache.http.impl.client.AbstractResponseHandler.handleResponse(AbstractResponseHandler.java:69)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:65)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:51)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:222)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:139)
at Jira.Auto_Defect.JiraBug.makeRequest(JiraBug.java:58)
at Jira.Auto_Defect.JiraBug.main(JiraBug.java:70)

Is there anything that I'm missing here?

ItsMeGokul
  • 403
  • 1
  • 5
  • 16
  • I don't see any authentication logic in your code. I'm going to guess that is probably the issue. It might also be helpful to test your raw requests with an REST client like Insomnia before translating them to code. That way you can easily see the raw response from the server (status code, error message, etc.) – kunruh Feb 23 '18 at 20:11
  • Parses the error message returned by the Bad Request (400), you will have more details on the issue with your request. However, you will need to modify a little bit how the request is executed, see https://stackoverflow.com/questions/15106324/how-to-get-the-body-of-a-40x-response for more details. – mtheriault Feb 27 '18 at 13:32

1 Answers1

1

Instead of using DefaultHttpClient you could very well use Spring's RestTemplate to achieve the same:

You could refer to my answer from a similar question where I have provided the working code.

Java Program to fetch custom/default fields of issues in JIRA

Hope this answers your question well!

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46