Reputation: 97
How to create an issue in Jira using the REST API? I have tried the examples using curl. But I need to create defect in Eclipse using Java and REST API.
Upvotes: 1
Views: 13632
Reputation: 41
try {
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter("username", "password"));
String input = "{\"fields\":{\"project\":{\"key\":\"DEMO\"},\"summary\":\"REST Test\",\"description\": \"Creating of an issue using project keys and issue type names using the REST API\",\"issuetype\":{\"name\":\"Bug\"}}}";
WebResource resource = client.resource("http://localhost:8080/rest/api/2/issue");
ClientResponse response = resource.type("application/json").accept("application/json").post(ClientResponse.class,input);
if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
System.out.println("Output from server");
System.out.println(response.getEntity(String.class));
} catch (Exception e) {
e.printStackTrace();
}
For more info:
https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-createIssue
http://www.j-tricks.com/tutorials/java-rest-client-for-jira-using-jersey
Upvotes: 0
Reputation: 153
Try this code
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: 0
Reputation: 2330
See: https://confluence.atlassian.com/display/IDEPLUGIN/Working+with+JIRA+Issues+in+Eclipse
Probably you'll need a REST client using the jersey-client artifact, I think this is the easiest way.
Firstly, check out the REST API documentation: https://docs.atlassian.com/jira/REST/latest/
With the POST method you can push a JSON object depiciting a wannabe issue to the JIRA server. You just have to exactly know what fields you can and should fill in. If you send fields that are not on the create issue screen, or is required but you haven't specified them, you'll get an error.
You can find an example here: http://pastebin.com/JeucUZNG
Upvotes: 1