【问题标题】:Create Work Item using Azure DevOps Rest API using C#使用 C# 使用 Azure DevOps Rest API 创建工作项
【发布时间】:2020-07-26 04:51:14
【问题描述】:

我无法使用 Work Items - Create 中提到的 Azure DevOps REST API 创建工作项

请求:

https://dev.azure.com/{organization}/MyTestProject/_apis/wit/workitems/$Task?api-version=6.0-preview.3

Request Body:
[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "value": "Task2"
  }
]

获取响应的代码(注意此代码适用于所有其他 POST 请求):

using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
      {
         response.EnsureSuccessStatusCode();
         JsonResponse = await response.Content.ReadAsStringAsync();
      }

Response: 400

有人可以推荐吗?

【问题讨论】:

  • 您是否尝试将 "from": null 元素添加到 JSON,如文档中所述?您是否按照文档中的说明将内容类型设置为 application/json-patch+json

标签: c# azure-devops restsharp


【解决方案1】:

查看您的完整示例可能会有所帮助。但是,这里是 Newtonsoft.Json 的工作示例(不要忘记您的 create personal access token):

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string PAT = "<personal access token>"; //https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page
            string requestUrl = "https://dev.azure.com/<my_org>/<my_project>/_apis/wit/workitems/$Task?api-version=5.0";
            try
            {
                List<Object> flds = new List<Object>
                {
                    new { op = "add", path = "/fields/System.Title", value = "Title" }
                };


                string json = JsonConvert.SerializeObject(flds);

                HttpClientHandler _httpclienthndlr = new HttpClientHandler();

                using (HttpClient client = new HttpClient(_httpclienthndlr))
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(
                System.Text.ASCIIEncoding.ASCII.GetBytes(
                string.Format("{0}:{1}", "", PAT))));


                    var request = new HttpRequestMessage(new HttpMethod("PATCH"), requestUrl)
                    {
                        Content = new StringContent(json, Encoding.UTF8, "application/json-patch+json")
                    };

                    HttpResponseMessage responseMessage = client.SendAsync(request).Result;
                }

            }
            catch (Exception ex)
            {

            }
        }
    }
}

另外,您可以考虑使用.NET client libraries for Azure DevOps and TFS。示例如下:Create a bug in Azure DevOps Services using .NET client libraries

【讨论】:

【解决方案2】:

application/json-patch+json 是必需的。

【讨论】:

  • 非常感谢您在这里分享解决方案。如果您能接受这一点会更好,以便其他人可以直接知道这个有效的解决方案:-)
猜你喜欢
  • 2021-03-19
  • 1970-01-01
  • 2021-05-30
  • 2019-12-27
  • 2021-02-22
  • 2020-07-06
  • 2020-05-09
  • 2022-07-17
  • 1970-01-01
相关资源
最近更新 更多