![](/img/trans.png)
[英]Create Work Item using Azure DevOps Rest API using C#
[英]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"
}
]
如文檔中所述:
請求
我正在嘗試使用API:
使用以下示例代碼:
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..
你能在這里幫我嗎?
提前致謝!
感謝您的答復。
@Michael建議的更改以及以下更改可以解決問題。
var postValue = new StringContent(body, Encoding.UTF8, "application/json");
將內容類型“ application / json-patch + json”更新為“ application / json”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.