2

Hey guys i am really struggling with this, i would like to create new JIRA issues using java through the REST API but every example i have seen is incomplete or doesnt work for me like this one: How to create an issue in jira using java rest api

Any help, sample code or link to the right direction would be greatly appreciated!

1 Answer 1

3

I think this sample code is helps u

This is totlly working for me

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

    Client client = Client.create();
    WebResource webResource = client.resource("http://localhost:8080/rest/api/latest/issue");                 

    String data = "{"fields":{"project":{"key":"DEMO"},"summary":"REST Test","issuetype":{"name":"Bug"}}}";

    String auth = new String(Base64.encode(Uname + ":" + Password));
    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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hello thanks for the comment and sorry fot the late reply can you tell me which libraries you are using?
use this maven discrepancies <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency>`
Hey i'm sorry i havent answered you sooner i was away the code you gave me seems to work as i get the right response but im not logged in i get this : <input type="hidden" title="ajaxUnauthorised" value="You are not authorized to perform this operation. Please log in."> Thanks a gain for all your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.