JSON 对象返回 400,尽管它看起来是正确的

问题描述 投票:0回答:0

为什么使用相同 JSON 的两个 POST 调用会返回不同的响应?

我正在 Blazor WASM 中构建一个项目,并通过 HttpClient 向 Jira API 发送 POST 请求。

创建一个普通的 comment 没有任何特殊的内容工作正常,但是当我想添加 hardBreak 或除普通文本之外的任何其他内容时,POST 返回 400。

System.InvalidOperationException:响应状态代码:400。响应内容:{“errorMessages”:[“INVALID_INPUT”],“errors”:{}}

我认为我的问题出在 C# 对象上

   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; }
        }

这两个类在序列化时都会返回相同的 JSON(至少在我看来),但底部的一个是唯一有效的。

那么,当 JSON 本身看起来时,为什么只有底部的一个起作用呢?

这是 JSON,两种情况看起来都一样

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

我尝试使 Text 可为空并将

[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
添加到 Text 属性,但没有产生任何影响。我还尝试过更改序列化选项。

c# rest blazor-webassembly jira-rest-api
© www.soinside.com 2019 - 2024. All rights reserved.