Why does two POST calls with the same JSON return different responses?
I am building a project in Blazor WASM and sending a POST request via HttpClient to the Jira API.
Creating a normal comment without anything special included works fine, but when I want to add a hardBreak or anything else other than normal text, the POST returns 400.
System.InvalidOperationException: Response Status Code: 400. Response Content: {“errorMessages”:[“INVALID_INPUT”],“errors”:{}}
I think my problem lies somewhere with the C# object
public class Content
{
[JsonProperty("type")]
public string Type { get; set; } = "";
[JsonProperty("text")]
public string? Text { get; set; }
}
public class Content
{
[JsonProperty("type")]
public string Type { get; set; } = "";
}
public class TextContent : Content
{
[JsonProperty("text")]
public string? Text { get; set; }
}
both of these classes when serialized returns the same JSON (to my eyes atleast) but the bottom one is the only one working.
So why does only the bottom one work when the JSON itself looks?
this is the JSON, looks the same in both cases
{
"body": {
"content": [
{
"content": [
{
"text": "Test",
"type": "text"
},
{
"type": "hardBreak"
},
{
"text": "Test",
"type": "text"
}
],
"type": "paragraph"
}
],
"type": "doc",
"version": 1
}
}
I've tried making the Text nullable and adding [JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
to the Text property, but it did not make a difference. I've also tried changing the serialization options.
Adding NullValueHandling = NullValueHandling.Ignore
to the Json.net serializer option did the trick for me.
var json = JsonConvert.SerializeObject(comment, Formatting.Indented, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
});
Another potential solution could be splitting the C# classes and then inherit from a base class, though i haven't tried it enough.
public class Content
{
[JsonProperty("type")] public string Type { get; set; } = "";
}
public class TextContent : Content
{
[JsonProperty("text")] public string? Text { get; set; }
}