簡體   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