Creating a new “JIRA issue” using REST API in java with maven

446 views Asked by At

I have tried to execute below code it's giving response Status code as 200 but it's not creating any issue in JIRA.

public static String invokePostMethod() throws AuthenticationException, ClientHandlerException, IOException {

        Client client = Client.create();
        WebResource webResource = client.resource("JIRA_URL");                 

        String data = "{\"fields\":{\"project\":{\"key\":\"test\"},\"summary\":\"test\",\"issuetype\":{\"name\":\"Bug\"}}},\"assignee\":{\"name\":\"QA\"}}}";

        String auth = new String(Base64.encode(("uname" + ":" + "pwd").getBytes(),0));
        ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json").accept("application/json").post(ClientResponse.class, data);
        int statusCode = response.getStatus();

        if (statusCode == 401) {
            throw new AuthenticationException("Invalid Username or Password");
        } else if (statusCode == 403) {
            throw new AuthenticationException("Forbidden");
        } else if (statusCode == 200 || statusCode == 201) {
            System.out.println("Ticket Create succesfully");
        } else {
            System.out.print("Http Error : " + statusCode);
        }
        // ******************************Getting Responce body*********************************************
        BufferedReader inputStream = new BufferedReader(new InputStreamReader(response.getEntityInputStream()));
        String line = null;
        while ((line = inputStream.readLine()) != null) {
            System.out.println(line);

        }
        return response.getEntity(String.class);
    }

I am also posting console output as below....

<input type="hidden" title="loggedInUser" value="userName">
<input type="hidden" title="ajaxTimeout" value="The call to the JIRA server did not complete within the timeout period.  We are unsure of the result of this operation.">
<input type="hidden" title="JiraVersion" value="6.0.4" />
<input type="hidden" title="ajaxUnauthorised" value="You are not authorized to perform this operation.  Please log in.">
<input type="hidden" title="baseURL" value="JIRA_URL">
<input type="hidden" title="ajaxCommsError" value="The JIRA server could not be contacted.  This may be a temporary glitch or the server may be down.">
<input type="hidden" title="ajaxServerError" value="The JIRA server was contacted but has returned an error response. We are unsure of the result of this operation.">
<input type="hidden" title="ajaxErrorCloseDialog" value="Close this dialog and press refresh in your browser">
<input type="hidden" title="ajaxErrorDialogHeading" value="Communications Breakdown">

<input type="hidden" title="dirtyMessage" value="You have entered new data on this page. If you navigate away from this page without first saving your data, the changes will be lost.">
<input type="hidden" title="dirtyDialogMessage" value="You have entered new data in this dialog. If you navigate away from this dialog without first saving your data, the changes will be lost. Click cancel to return to the dialog.">
<input type="hidden" title="keyType" value="Type">
<input type="hidden" title="keyThen" value="then">
<input type="hidden" title="dblClickToExpand" value="Double click to expand">
<input type="hidden" title="actions" value="Actions">
<input type="hidden" title="removeItem" value="Remove">
<input type="hidden" title="workflow" value="Workflow">
<input type="hidden" title="labelNew" value="New Label">
<input type="hidden" title="issueActionsHint" value="Begin typing for available operations or press down to see all">
<input type="hidden" title="closelink" value="Close">
<input type="hidden" title="dotOperations" value="Operations">
<input type="hidden" title="dotLoading" value="Loading...">
<input type="hidden" title="frotherSuggestions" value="Suggestions">
<input type="hidden" title="frotherNomatches" value="No Matches">
<input type="hidden" title="multiselectVersionsError" value="{0} is not a valid version.">
<input type="hidden" title="multiselectComponentsError" value="{0} is not a valid component.">
<input type="hidden" title="multiselectGenericError" value="The value {0} is invalid.">

Can anybody please look into this and let me know if i can improve my code...?

0

There are 0 answers