Jira API v2 bad request using special caracteres

Hello!

I’m using the Jira API v2 to create an issue. I’m facing a trouble when I send a string with special caracteres on description filed.

Here the example that is working for me when I send a POST method to rest/api/2/issue:

var dataToCreateAnIssue = "{\"fields\":{\"project\":{\"key\":\"" + key + "\"},\"summary\":\"" + title + "\",\"description\":\"" + descp+ "\",\"issuetype\":{\"name\":\"Bug\"}}}";

But when I have special caracteres like “>” or “<” in my string descp, I got the error ‘The remote server returned an error: (400) Bad Request.’
I need to automate it, and my string will always have various special caracteres.

How can I resolve this? Any suggestion?

Hi @RafaelGustavoPanizzo,
You seems to be working in JavaScript so I suggest you try to build you object in a standard way then turning it to JSON.

var dataToCreateAnIssue = { fields: {
project: { key: key },
summary: title,
description: descp,
issuetype: {name:"Bug"}
};
var jsonPayload = JSON.stringify(datatoCreateAnIssue);

See here for more information on stringify.

Good luck!

Hi @PascalPerreault thanks for the reply.

I’m using c# language. I try to do your tip but still don’t works. The server still return bad request.

Here is what I tryed:

        public class Project
        {
            public string key { get; set; }
        }

        public class Issuetype
        {
            public string name { get; set; }
        }

        public class Fields
        {
            public Fields()
            {
                project = new Project();
                issuetype = new Issuetype();
            }

            public Project project { get; set; }
            public string summary { get; set; }
            public string description { get; set; }
            public Issuetype issuetype { get; set; }
        }

        public Fields fields { get; set; }
            fields = new Fields();
            fields.project.key = key;
            fields.summary = strings[0];
            fields.description = strings[1];
            fields.issuetype.name = "Bug";

            var dataToCreateAnIssue = JsonConvert.SerializeObject(fields);

The problem is not how the Json is being sent, because, when I use this code below server return OK, the post works:
var dataToCreateAnIssue = "{\"fields\":{\"project\":{\"key\":\"" + key + "\"},\"summary\":\"" + title + "\",\"description\":\"" + descp+ "\",\"issuetype\":{\"name\":\"Bug\"}}}";

The problem is when my variable descp has special caracteres. Then I got the bad request from server, and the POST method don’t works.

Hello again!

I’ll try to be more objective in my question.

https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-issues/#api-rest-api-2-issue-post

Here is a part of the example from the atlassian link:

"description": "Order entry fails when selecting supplier.",

I’m able to create the issue through the API, but when I pass in the “description” field, some strings with special characters, the server returns BAD REQUEST.

I have not been able to identify which characters (or which combination of characters) are causing this problem.

Can anyone tell me that?

We are already using the API here at the company with this limitation, but I have to send the string removing the special characters.