Jira post error - expecting comma to separate OBJECT entries

Hi,

I am trying to use the below code to create new project issue in my jira,


using System.Text;
using System.Net.Http;
using System.Json;
using System.Web.Script.Serialization;

namespace Test
{
    class Class1
    {
        public void CreateIssue()
        {
            string message = "Hai \"!Hello\" ";
            string data = "{\"fields\":{\"project\":{\"key\":\"TP\"},\"summary\":\"" + message +    "\",\"issuetype\":{\"name\": \"Bug\"}}}";
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            client.DefaultRequestHeaders.ExpectContinue = false;
            client.Timeout = TimeSpan.FromMinutes(90);
            byte[] crdential = UTF8Encoding.UTF8.GetBytes("adminName:adminPassword");
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(crdential));
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));     
            System.Net.Http.HttpContent content = new StringContent(data, Encoding.UTF8, "application/json");
            try
            {
                client.PostAsync("http://localhost:8080/rest/api/2/issue",content).ContinueWith(requesTask=>
                {
                    try
                    {
                        HttpResponseMessage response = requesTask.Result;
                        response.EnsureSuccessStatusCode();
                        response.Content.ReadAsStringAsync().ContinueWith(readTask =>
                        {
                            var out1 = readTask.Result;
                        });
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine(exception.StackTrace.ToString());
                        Console.ReadLine();
                    }
                });
            }
            catch (Exception exc)
            {
            }
        }
    }
}



It throws the issue like below:

"{\"errorMessages\":[\"Unexpected character ('!' (code 33)): was expecting comma to separate OBJECT entries\\n at [Source: org.apache.catalina.connector.CoyoteInputStream@1ac4eeea; line: 1, column: 52]\"]}"

But i use the same json file in Firefox Poster , i have created the issue.

Json file : {"fields":{"project":{"key":"TP"},"summary":"Hai \"!Hello\" ",\"issuetype\":{\"name\": \"Bug\"}}}"

1 answer

1 accepted

0 vote
Penny Wyatt Atlassian Team May 12, 2013

The issue is that you are not JSON-encoding the message when concatenating it into the string.

This line of Java:

string message = "Hai \"!Hello\" ";

Will put this value into memory:

Hai "!Hello"

And hence this line of Java:

string data = "{\"fields\":{\"project\":{\"key\":\"TP\"},\"summary\":\"" + message + "\",\"issuetype\":{\"name\": \"Bug\"}}}";

Will put this value into memory:

{"fields":{"project":{"key":"TP"},"summary":"Hai "!Hello"","issuetype":{"name": "Bug"}}}

You can see there's a syntax error there in the summary part - the double quotes inside the message are breaking your JSON.

Rather than generating JSON by string concatenation, which is pretty much guaranteed to give you functionality and security issues, I recommend using a JSON library instead. We use Jersey in JIRA but any would do.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

59 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you