1

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

1

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the reply and my request is i need to create issue in jira using json and rest api but im getting 400 https error i think my string Entity hase error how can i change my String Entity
My apologies for not being explicit enough. Change '}}}"); to '}}}".replaceAll("'", "\"")); and it should get you much closer to your goal

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.