Creating comment gives me 400 INVALID_INPUT

Hello!
I have a problem that I can’t wrap my head around. I am building a project in Blazor WASM, which has been going well so far. I am trying to make a POST request to the API to create a comment on an issue. I’ll provide two examples: one that works and one that does not.

Both examples below will give me a 200 OK response with a new comment on the correct issue.

    var requestObj = new JiraCreateCommentModel.Root()
            {
                Body = new JiraCreateCommentModel.Body()
                {
                    Type = doc,
                    Version = 1,
                    Content = new List<JiraCreateCommentModel.ContentWrapper>()
                    {
                        new()
                        {
                            Type = "paragraph",
                            Content = new List<JiraCreateCommentModel.Content>
                            {
                               new()
                                {
                                    Type = "text",
                                    Text = "John doe"
                                },
                                new()
                                {
                                    Type = "text",
                                    Text= "1234",
                                }
                            }
                        }
                    }
                }
            };

JSON that gets sent to the endpoint

{
  "body": {
    "content": [
      {
        "content": [
          {
            "type": "text",
            "text": "John doe"
          },
          {
            "type": "text",
            "text": "1234"
          }
        ],
        "type": "paragraph"
      }
    ],
    "type": "doc",
    "version": 1
  }
}



Now, this won't look very good because the two texts will end up looking like this:

John Doe1234

So, a new line would be nice. I use the hardBreak node to help me add a new line.

System.InvalidOperationException: Response Status Code: 400. Response Content: {“errorMessages”:[“INVALID_INPUT”],“errors”:{}}


Womp womp, it did not work. Pasted below is the C# and JSON that was sent the second time around, resulting in the 400 error. I’ve tried pasting both of the JSONs to the Document viewer and they both worked. I’ve also tried with other nodes, such as the textcolor as well.

    var requestObj = new JiraCreateCommentModel.Root()
            {
                Body = new JiraCreateCommentModel.Body()
                {
                    Type = "doc",
                    Version = 1,
                    Content = new List<JiraCreateCommentModel.ContentWrapper>()
                    {
                        new()
                        {
                            Type = "paragraph",
                            Content = new List<JiraCreateCommentModel.Content>
                            {
                                new()
                                {
                                    Type = "text",
                                    Text= "John doe",
                                },
                                new()
                                {
                                    Type = "hardBreak",
                                },
                                new()
                                {
                                    Type = "text",
                                    Text= "1234",
                                }
                            }
                        }
                    }
                }
            };

JSON that gets sent to the endpoint

{
  "body": {
    "content": [
      {
        "content": [
          {
            "type": "text",
            "text": "John doe"
          },
          {
            "type": "hardBreak",
          },
          {
            "type": "text",
            "text": "1234"
          }
        ],
        "type": "paragraph"
      }
    ],
    "type": "doc",
    "version": 1
  }
}

C# classes that gets serialized

 public class Root
        {
            [JsonProperty("body")] 
            public Body Body { get; set; }
        }
        public class Content
        {
            [JsonProperty("type")] 
            public string Type { get; set; } = "";
            [JsonProperty("text")]
            public string? Text { get; set; }
        }

        public class Body
        {
            [JsonProperty("content")] 
            public List<ContentWrapper> Content { get; set; }
            
            [JsonProperty("type")] 
            public string Type { get; set; } = "";
            
            [JsonProperty("version")] 
            public int Version { get; set; }
        }

        public class ContentWrapper
        {
            [JsonProperty("content")] 
            public List<Content> Content { get; set; }
            
            [JsonProperty("type")] 
            public string Type { get; set; } = "";
        }

If anyone got an idea to why this does not work, it would be much appreciated.

Hello @AlexanderFagrelius

The 400 error is correct… the request body contains invalid JSON. There is an errant comma after the hardBreak object, indicating another object is following in the set, but there is no following object in that set.

Remove the comma, thus, and it will work:

{
    "version": 1,
    "type": "doc",
    "content": [
      {
        "type": "paragraph",
        "content": [
          {
            "type": "text",
            "text": "John doe"
          },
          {
            "type": "hardBreak"  <-- No comma here
          },
          {
            "type": "text",
            "text": "1234"
          }
        ]
      }
  ]
}

Testing your JSON with a tool, like this one, will immediately show you where the problem lies.

Tools like this one will even fix the faulty JSON and tell you where the fault was.

Once the JSON is correct, you can use the Document Viewer tool to test if the ADF is structured correctly.

Hi @sunnyape

Thanks for replying, the 400 error remains the same even after removing the errant comma. The JSON works fine in the Document Viewer."

I have just tested the exact JSON body I have provided to you in a request to the Add comments endpoint using Postman, and the request worked perfectly:

With the resulting comment being added to the Issue

  • Are you 100% sure the JSON being created by your code doesn’t have something else hidden in it, like some other errant character somewhere.
  • What was the specific result you got when you tested the JSON output of your code using one of the two JSON test tools I advised you of?
  • What happens when you test the JSON output of your code against the endpoint with a REST API test tool like Postman?
  • What other testing have you done outside this ‘Blazor WASM’ framework thing (sorry, have no idea what it is). Have you tried the native .Net HttpClient class? This Blazor WASM thing might be at fault.
  • Are you 100% sure you’re not second-guessing what the JSON output of your code actually is?
    For example, you said the declaration in your code is:
    new() {Type = "text", Text= "John doe",},
    which has an errant comma after the John doe object.
    … but, you say the resulting JSON is:
    {"type": "text", "text": "John doe"},
    so somehow that errant comma was found and removed by something. I am doubtful that the .net JSON serialiser knows how to do that, but I don’t know it well enough to be authoritative.

If in doubt, ask a peer to review your work. You may just be missing something obvious that you’ve not disclosed here.

I have also tested it in Postman, and like yours, it works fine. I have located the problem, which, for me, is pretty weird. So if someone has an explanation for it, I’d be more than happy to hear about it.

This is the content that holds the type and text, which is described in the documentation at Add comment.

      "content": [
          {
            "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget venenatis elit. Duis eu justo eget augue iaculis fermentum. Sed semper quam laoreet nisi egestas at posuere augue semper.",
            "type": "text"
          }

And this is the hardBreak described i in the documentation.

  "content": [
        {
          "type": "text",
          "text": "Hello"
        },
        {
            "type": "hardBreak"
          },
        {
          "type": "text",
          "text": "world"
        }
      ]

I posted the C# classes earlier, but we will focus on the part that holds the required values. I made Text optional because it’s not required in the HardBreak object.

   public class Content
        {
            [JsonProperty("type")] 
            public string Type { get; set; } = "";
            
            [JsonProperty("text")] 
            public string? Text { get; set; }

        }

Serializeing the C# class which i posted in the original post (minus the errent comma), will give me a valid JSON which works totaly fine in Postman but not when im doing a Http request.

The problem turned out to be the C# class. Even though the JSON looks fine, it still thinks that the text is present in the hardBreak object aswell, i confirmed this by only posting the {"type": "hardBreak"} object and removing the JsonProperty("text")] public string? Text { get; set; } completely from the C# Content class