简体   繁体   English

使用 REST API 在 Azure DevOps 中创建工作项 - 找不到状态 404

[英]Creating workitem in Azure DevOps using REST API - Status 404 not found

I have been trying to create work items in Azure DevOps.我一直在尝试在 Azure DevOps 中创建工作项。 But got an error of "404 not found"但是出现“404 not found”的错误

ItemType is -Task with $ in prefix. ItemType 是 -Task 前缀为 $。 When I try to get the specific work item number in the url (get request) I get other items, so I know for sure that the org + project and the rest of the URI is correct.当我尝试获取 url(获取请求)中的特定工作项编号时,我得到了其他项目,所以我确定 org + 项目和 URI 的 rest 是正确的。

What could be the issue?可能是什么问题? (I gave full access permissions as well when generated the token (PAT)) (在生成令牌(PAT)时,我也授予了完全访问权限)

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

namespace TaskCreator
{
    static class Azure
    {
        public const string BASE = "https://dev.azure.com";
        public const string PAT = "XXX";
        public const string ORG = "Org2";
        public const string API = "api-version=6.0";
        public const string PROJECT = "Project2";
        public const string WIT_TYPE = "$Task";
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Create and initialize HttpClient instance.
            HttpClient client = new HttpClient();

            // Set Media Type of Response.
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Generate base64 encoded authorization header.
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "", Azure.PAT))));

            // Build the URI for creating Work Item.
            string uri = String.Join("?", String.Join("/", Azure.BASE, Azure.ORG, Azure.PROJECT, "_apis/wit/workitems", Azure.WIT_TYPE), Azure.API);

            // Create Request body in JSON format.
            string json = "[{ \"op\": \"add\", \"path\": \"/fields/System.Title\", \"from\": null, \"value\": \"REST API Demo task\"}]";
            HttpContent content = new StringContent(json, Encoding.UTF8, "application/json-patch+json");

            // Call CreateWIT method.
            string result = CreateWIT(client, uri, content).Result;

            // Pretty print the JSON if result not empty or null.
            if (!String.IsNullOrEmpty(result))
            {
                dynamic wit = JsonConvert.DeserializeObject<object>(result);
                Console.WriteLine(JsonConvert.SerializeObject(wit, Formatting.Indented));
            }

            // Presss any key to exit
            Console.ReadLine();
            client.Dispose();
        }

        public static async Task<string> CreateWIT(HttpClient client, string uri, HttpContent content)
        {
            try
            {
                // Send asynchronous POST request.
                using (HttpResponseMessage response = await client.PostAsync(uri, content))
                {
                    response.EnsureSuccessStatusCode();
                    return (await response.Content.ReadAsStringAsync());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return string.Empty;
            }
        } // End of CreateWIT method
    }
}

From UI, you can check whether you are able to create task work item.从 UI 中,您可以检查是否能够创建任务工作项。 I encountered a similar behavior when the Task workitem was greyed out at the UI level.当任务工作项在 UI 级别灰显时,我遇到了类似的行为。

Also, check the code with other workitem types.此外,请检查其他工作项类型的代码。 (for instance bug) (例如错误)

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

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