Reputation: 65
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!
Upvotes: 2
Views: 4244
Reputation: 153
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);
}
Upvotes: 3