You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C# HttpClient模拟YouTrack创建工单的curl POST请求返回500如何解决

问题根源

  • 你的JSON格式确实存在错误,这是触发500错误的核心原因:
    你当前提交的JSON中project字段的值是转义后的字符串类型:
{"description":"Created using REST Api","project":"{\"id\":\"67-74\"}","summary":"Test Issue"}

而对照你给出的正常cURL请求参数,YouTrack的API要求project字段必须是一个包含id属性的JSON对象,不能是字符串格式。

修复方案

1. 直接修正JSON格式

把project字段外层的引号和多余转义去掉,修正后的JSON如下:

{"description":"Created using REST Api","project":{"id":"67-74"},"summary":"Test Issue"}

2. C#代码优化建议

不要手动拼接/转义JSON,极易出现格式错误,建议使用官方序列化库自动生成JSON:
首先定义请求对应的实体类:

// 用System.Text.Json无需额外安装包,用Newtonsoft.Json需先安装对应NuGet包
public class YouTrackIssueCreateRequest
{
    public string description { get; set; }
    public ProjectDto project { get; set; }
    public string summary { get; set; }
}

public class ProjectDto
{
    public string id { get; set; }
}

调用时序列化生成请求参数:

var requestData = new YouTrackIssueCreateRequest
{
    description = "Created using REST Api",
    project = new ProjectDto { id = "67-74" },
    summary = "Test Issue"
};
// System.Text.Json 序列化示例
string postParams = System.Text.Json.JsonSerializer.Serialize(requestData);

3. 额外优化点

你当前代码中每次请求都新建HttpClient实例,长期运行会导致套接字资源耗尽,建议改为使用全局静态HttpClient实例,或者通过依赖注入获取IHttpClientFactory创建的客户端。

内容的提问来源于stack exchange,提问作者Kliver Max

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 01:09:02