1

I am trying to post a discussion comment to a work item using the REST API version=5.1-preview.3.

Summary

Type: POST and C# HttpClient

However no matter how i try to work with it, i always get the response:

StatusCode: 415, ReasonPhrase: 'Unsupported Media Type'

The inner response is something like this:

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

I send the data/comment in the following JSON format:

[
  {
    "text": "Test Comment"
  }
]

As mentioned in the docs :

https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/comments/add?view=azure-devops-rest-5.1#examples

Request

I am trying to hit the API:

POST https://dev.azure.com/fabrikam/Fabrikam-Fiber-Git/_apis/wit/workItems/299/comments?api-version=5.1-preview.3

using the below sample code:

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;
      }
}

I believe the above snippet should be able to add a comment to the work item. However no matter how i try to work with it, i always get the response:

Response

StatusCode: 415, ReasonPhrase: 'Unsupported Media Type'

The inner response is something like this:

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

Could you please help me here?

Thanks in advance!

2 Answers2

2

Thank you for the response.

The changes suggested by @Michael along with the following change did the trick.

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

Updated the content type "application/json-patch+json" to "application/json".

0

Here is a similar thread discussing this issue on Developer Community.

I agree with Michael's comment above and it works without the enclosing [], which does not align with the example provided in the official REST API documentation.

I would recommend you to open an issue here and get this corrected in the docs as well. You might also open an issue here to get immediate assistance from the Azure DevOps Team.

Thanks for bringing this to our attention!

Bhargavi Annadevara
  • 2,802
  • 1
  • 6
  • 19