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