繁体   English   中英

尝试使用Azure DevOps REST API将讨论'评论'发表到工作项时出错

[英]Error when trying to post a discussion 'comment' to a work item using the Azure DevOps REST API

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

摘要

类型:POST和C#HttpClient

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

StatusCode: 415, ReasonPhrase: 'Unsupported Media Type'

内部响应是这样的:

{"$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://docs.microsoft.com/zh-cn/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'

内部响应是这样的:

{"$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..

你能在这里帮我吗?

提前致谢!

是在Developer Community上讨论此问题的类似主题。

我同意Michael在上面的评论,它不带[] ,它与官方REST API文档中提供的示例不一致。

我建议您在这里打开一个问题并在文档中也将其更正。 您可能还会在此处打开一个问题以立即获得Azure DevOps团队的帮助。

谢谢让我们注意到这个!

感谢您的答复。

@Michael建议的更改以及以下更改可以解决问题。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM