简体   繁体   中英

How to Create a Issue into JIRA thorugh REST api in java?

I am sending the POST request to the JIRA with my json data for create a project, but i am unable to create a project into JIRA, i tried to see the error from the Fiddler and i got following error. I am using the java.

I am posting json data from the following code, please suggest what and where i am wrong ?

   public  static  void createTicket()throws  Exception{

    HttpClient client = new DefaultHttpClient();
    try {
        HttpPost post = new HttpPost(webPage);
        Base64 b = new Base64();

        String encoding = b.encodeAsString(new String("" + name + ":" + password + "").getBytes());
        post.addHeader("Authorization","Basic "+encoding);
        System.out.println(post.toString());


        StringEntity params =new StringEntity("{'fields':{'project':{'key':'LCH-12'},'summary':'Sample issue','description':'Creating an issue via REST API','issuetype':{'name':'Bug'}}}");

       post.setHeader("Content-type", "application/json");
       post.setEntity(params);
        HttpResponse httpResponse = client.execute(post);

        System.out.println(httpResponse.toString());

    }
    catch(Exception e)
    {

        System.out.println(e);
    }
    finally {
        httpClient.getConnectionManager().shutdown(); //Deprecated
    }

}

Error Message is following:

POST http://localhost:8080/rest/api/latest/issue HTTP/1.1 [Content-Type: text/plain; charset=ISO-8859-1,Content-Length: 140,Chunked: false] HTTP/1.1 400 Bad Request [Server: Apache-Coyote/1.1, X-AREQUESTID: 585x38x1, X-ASEN: SEN-L8200978, Set-Cookie: JSESSIONID=2DFEB40B67B19BAFAFE81CF8A30B5A88; Path=/; HttpOnly, X-Seraph-LoginReason: OK, Set-Cookie: atlassian.xsrf.token=B6XG-UN93-PXWZ-FOQK|f80007dcf150dceaa1d5f561aab3d364a3598178|lin; Path=/, X-ASESSIONID: 5i51ds, X-AUSERNAME: vinuvish1, Cache-Control: no-cache, no-store, no-transform, X-Content-Type-Options: nosniff, Content-Type: application/json;charset=UTF-8, Transfer-Encoding: chunked, Date: Thu, 14 Jul 2016 04:15:23 GMT, Connection: close] org.apache.http.conn.BasicManagedEntity@4b952a2d

Based on the code snippet you posted, the problem is that JSON does not tolerate single quotes as field delimiters.

By far the easiest approach in a Java system is to use one of the many, many JSON libraries for Java to ensure the things are quoted as JSON expects them to be.

Also, as a friendly FYI, there is no need to do new String("" + name + ":" + password + "") because Java automatically creates a new String from the concatenation of the String literals and the values. So String str = ""+name+":"+password; is all you need. Then you can call str.getBytes() if you need byte[] to go into the StringEntity , although new StringEntity(str) would be much clearer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM