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

时间:2019-08-09 09:49:00

标签: c# azure azure-devops devops azure-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/en-us/rest/api/azure/devops/wit/comments/add?view=azure-devops-rest-5.1#examples

请求

我正在尝试使用API​​:

POST 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..

您能在这里帮我吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

Here是与开发者社区讨论此问题的类似话题。

我同意迈克尔在上面的评论,它不带括号[]也可以使用,这与官方REST API文档中提供的示例不符。

我建议您打开一个问题here,并在文档中更正此问题。您也可以打开一个问题here,以获取Azure DevOps团队的即时帮助。

感谢您引起我们的注意!

答案 1 :(得分:0)

感谢您的回复。

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

<!-- Include the library in the page -->
<script src="https://unpkg.com/vue/dist/vue.min.js"></script>
<script src="https://unpkg.com/vue-router"></script>

<!-- App -->
<div id="app">
  <nav>
    <router-link :to="{ name: 'home' }" exact>Home</router-link>
       <router-link :to="{ name: 'about' }" @click.native.prevent="router.push({ name: 'about' })">About</router-link>
  </nav>

  <router-view :key="$route.fullPath"></router-view>
</div>

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