0
votes

I am trying to call the Azure DevOps Release Api to create a release from c# code, but it is giving me 400 Bad request error. I am using the example in the following link by Microsoft

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

Here is my code ..... My application is a small console app.

    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

1 Answers

0
votes

Your code does not work on my side and I`ve checked the url. There is some problem %)

enter image description here

Try to use the url format from documentation:

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

Additionally, you can check the error in your response:

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

My example:

enter image description here