gpt4 book ai didi

c# - 尝试使用 Azure DevOps REST API 将讨论 'comment' 发布到工作项时出错

转载 作者:行者123 更新时间:2023-12-03 00:48:30 24 4
gpt4 key购买 nike

我正在尝试使用 REST API version=5.1-preview.3 向工作项发布讨论评论。

摘要

类型:POST 和 C# HttpClient

但是,无论我如何尝试使用它,我总是得到响应:

StatusCode: 415, ReasonPhrase: 'Unsupported Media Type'

内心的 react 是这样的:

{"$id":"1","innerException":null,"message":"TF400898: An Internal Error Occurred. Activity Id: d634683c-0b2e-4bfb-9a66-ee99f32404c6.","typeName":"System.Web.Http.HttpResponseException, System.Web.Http","typeKey":"HttpResponseException..

我以以下 JSON 格式发送数据/评论:

[
{
"text": "Test Comment"
}
]

正如文档中提到的:

https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/comments/add?view=azure-devops-rest-5.1#examples

请求

我正在尝试访问 API:

发布https://dev.azure.com/fabrikam/Fabrikam-Fiber-Git/_apis/wit/workItems/299/comments?api-version=5.1-preview.3

使用以下示例代码:

public class Comment
{
[JsonProperty("text")]
public string Text { get; set; }
}
var comment = new Comment()
{
Text = "Test Comment"
};

var comments = new List<Comment>();
comments.Add(comment);

var body = JsonConvert.SerializeObject(comments);

var postValue = new StringContent(body, Encoding.UTF8, "application/json-patch+json");


using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", _token))));

using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage(new HttpMethod("POST"), _apiUrl) { Content = postValue })
{
var httpResponseMessage = httpClient.SendAsync(httpRequestMessage).Result;
}
}

我相信上面的代码片段应该能够向工作项添加评论。然而,无论我如何尝试使用它,我总是得到响应:

回应

StatusCode: 415, ReasonPhrase: 'Unsupported Media Type'

内心的 react 是这样的:

{"$id":"1","innerException":null,"message":"TF400898: An Internal Error Occurred. Activity Id: d634683c-0b2e-4bfb-9a66-ee99f32404c6.","typeName":"System.Web.Http.HttpResponseException, System.Web.Http","typeKey":"HttpResponseException..

你能帮我一下吗?

提前致谢!

最佳答案

感谢您的回复。

@Michael 建议的更改以及以下更改起到了作用。

var postValue = new StringContent(body, Encoding.UTF8, "application/json");

将内容类型“application/json-patch+json”更新为“application/json”。

关于c# - 尝试使用 Azure DevOps REST API 将讨论 'comment' 发布到工作项时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57427514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com