Azure DevOps 发布 Api 400 错误请求错误

Posted

技术标签:

【中文标题】Azure DevOps 发布 Api 400 错误请求错误【英文标题】:Azure DevOps Release Api 400 Bad Request error 【发布时间】:2020-06-09 13:18:19 【问题描述】:

我正在尝试调用 Azure DevOps Release Api 从 c# 代码创建一个版本,但它给了我 400 Bad request 错误。 我正在使用 Microsoft 的以下链接中的示例

https://docs.microsoft.com/en-us/rest/api/azure/devops/release/releases/create?view=azure-devops-rest-5.1

这是我的代码..... 我的应用程序是一个小型控制台应用程序。

    namespace DevOpsReleasePipelineTest
    
        public class InstanceReference
        
            public string id  get; set; 
            public IList<string> name  get; set; 

            public string definitionId  get; set; 

        
        public class Artifacts
        
            public string alias  get; set; 
            public InstanceReference instanceReference  get; set; 

        
        public class Application
        
            public int definitionId  get; set; 
            public string description  get; set; 
            public IList<Artifacts> artifacts  get; set; 
            public bool isDraft  get; set; 
            public string reason  get; set; 
            public IList<object> manualEnvironments  get; set; 

        
    





    private static Application GetPayLoad()
            
                InstanceReference instanceReference = new InstanceReference();
                instanceReference.id = "7874";
                instanceReference.name = null;
                instanceReference.definitionId = "7874";

                List<Artifacts> artifacts = new List<Artifacts>();
                Artifacts artifacts1 = new Artifacts();
                artifacts1.alias = "Mobility-Dev";
                artifacts1.instanceReference = instanceReference;
                artifacts.Add(artifacts1);



                Application application = new Application();
                application.definitionId = 4;
                application.description = "Creating Sample release";
                application.isDraft = false;
                application.reason = "test";
                application.manualEnvironments = null;
                application.artifacts = artifacts;

                return application;

            





public static async Task<HttpResponseMessage> PostRelease()
        
            var personalaccesstoken = "djhghgtydfhfgdyuftyftdsf";

            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Accept.Add(
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(
                    System.Text.ASCIIEncoding.ASCII.GetBytes(
                        string.Format("0:1", "", personalaccesstoken))));       


            var payLoad = GetPayLoad();
            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(payLoad), 
            Encoding.UTF8, "application/json");



            Task <HttpResponseMessage> response = client.PostAsync("https://xxx- 
            devops.vsrm.visualstudio.com/DemoProj/_apis/release/releases?api-version=5.1", httpContent);
            var result = await response;
            return result;

        

【问题讨论】:

【参考方案1】:

您的代码在我这边不起作用,我已经检查了网址。有问题%)

尝试使用文档中的 url 格式:

POST https://vsrm.dev.azure.com/organization/project/_apis/release/releases?api-version=5.1

此外,您可以检查您的回复中的错误:

var response = client.PostAsync("https://vsrm.dev.azure.com/<org>/<team_project>/_apis/release/releases?api-version=5.1", httpContent).Result;

if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)

    string message = response.Content.ReadAsStringAsync().Result;
    Console.WriteLine(message);

我的例子:

【讨论】:

感谢您的回复。我正在分析我的 httpResponseMessage ,我发现我在我的帖子有效负载中提供的别名值是错误的。现在它已经修复了。

以上是关于Azure DevOps 发布 Api 400 错误请求错误的主要内容,如果未能解决你的问题,请参考以下文章

无需用户代表从自定义 azure webapp api 调用 azure DevOps API

尝试使用 Azure DevOps Rest API 创建发布定义时出错

使用 C# 中的 azure devops API 在发布和构建定义中更新代理池

Azure DevOps REST API - 选项列表如何与字段关联?

如何使用 Azure DevOps REST Api 编写构建计划脚本?

Azure DevOps REST API - 创建工作项 - “需要一个值”