简体   繁体   English

使用 Azure DevOps 创建工作项 Rest API 使用 ZD7EFA19FBE7D2372FD5ADB6024

[英]Create Work Item using Azure DevOps Rest API using C#

I am unable to create Work Item using Azure DevOps REST API as mentioned in Work Items - Create我无法使用 Azure DevOps REST API 创建工作项,如工作项 - 创建

Request:要求:

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"
  }
]

Code to Get Response (Note this code works for all other POST Requests):获取响应的代码(注意此代码适用于所有其他 POST 请求):

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

Response: 400

Can someone please suggest?有人可以建议吗?

It might be helpful to see your full example.查看您的完整示例可能会有所帮助。 However, here is working example with Newtonsoft.Json (do not forget to your create personal access token ):但是,这里是Newtonsoft.Json的工作示例(不要忘记创建个人访问令牌):

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)
            {

            }
        }
    }
}

Additionally, you can consider to use .NET client libraries for Azure DevOps and TFS .此外,您可以考虑将.NET 客户端库用于 Azure DevOps 和 TFS Here is the example: Create a bug in Azure DevOps Services using .NET client libraries以下是示例: 使用 .NET 客户端库在 Azure DevOps 服务中创建一个错误

application/json-patch+json is required.应用程序/json-patch+json 是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM