2
votes

I want to create an issue in JIRA via REST API call. I got a sample code and JSON text. When I executed the below code there is no error, but also no issue is created in JIRA. I have used the same JSON code to create issue in JIRA with "CURL" command. But I failed to create with this sample code below. Anyone please help on this.

    String host = "localhost";
    int port = 8080;
    String userName = "admin";
    String password = "admin";
    DefaultHttpClient httpClient = new DefaultHttpClient();
    String jsonObj = "{\"fields\":" +
            "{\"project\":{\"key\": \"JAVA\"}," +
            "\"summary\":\"Creating issue in JIRA.\"," +
            "\"description\": \"Creating of an issue using project keys and issue type names using the REST API\", " +
            "\"issuetype\": {\"name\":\"Bug\"}}}";


    httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port), 
            new UsernamePasswordCredentials(userName, password));

    HttpPost httpPost = new HttpPost("http://localhost:8080/rest/api/2/issue/");
    StringEntity entity = new StringEntity(jsonObj);
    entity.setContentType("application/json");
    httpPost.setEntity(entity);
    HttpResponse httpResponse = httpClient.execute(httpPost);
    httpClient.getConnectionManager().shutdown();
1
It's difficult to read your JSON like that. Can you paste in the content, separately? - user565869
Hi Sadeepa, Your json data is not properly serialized, first serialize it than use, it will work. - Ashish-BeJovial

1 Answers

0
votes

try with this code...

    String project ="JAVA";
    String summary ="your summary";
    String description ="your description";
    String issueType ="Bug";

 String jsonObj ="{\"fields\": { \"project\": { \"key\": " + "\"" + project + "\"}, "+
    "\"summary\": " + "\"" + summary + "\", "+
    "\"description\": " + "\"" + description + "\", "+
    "\"issuetype\": { \"name\": \"" + issueType + "\"}}} "