[英]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.