POST vsts 在 C# 中发布 api 端点

POST to vsts release api endpoint in C#

我想向 vsts create release api 发出 POST 请求,示例: POSThttps://fabrikam.vsrm.visualstudio.com/MyFirstProject/_apis/release/releases?api-version=4.1-preview.6

使用以下请求正文:

{
  "definitionId": 1,
  "description": "Creating Sample release",
  "artifacts": [
    {
      "alias": "Fabrikam.CI",
      "instanceReference": {
        "id": "2",
        "name": null
      }
    }
  ],
  "isDraft": false,
  "reason": "none",
  "manualEnvironments": null
}

任何人都可以给我一些指导,让我在 C# 中进行相同的编码。我现在迷路了。

亲切的问候。

编辑1: 我有下面的代码来获取响应,我想对它做一个 POST:

class Program    
{    
    public async void GetProjects()
    {
        try
        {
            var personalaccesstoken = "PAT";

            using (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))));

                using (HttpResponseMessage response = client.GetAsync(
                            "https://sample.vsrm.visualstudio.com/MyFirstProject/_apis/release/releases?api-version=4.1-preview.6").Result)


                    //POST to response with Json body

                {
                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(responseBody);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

    static void Main(string[] args)
    {

        Program prog = new Program();
        prog.GetProjects();    
    }                
}

好的,我能够在 RestSharp 的帮助下做到这一点:

 var client = new RestClient("https://demouser.vsrm.visualstudio.com/MyFirstProject/_apis/release/releases?api-version=4.1-preview.6");
            var request = new RestRequest(Method.POST);
            request.AddHeader("Authorization", "Basic TWFuaXNoX0JpbHVuZ0BhZC5pbmZvc3lzLmNvbTpqbXZzNzQzeTZzcjR2cWFjY3Y0ZnpmbXk1ZTNra2tiYm03aDJjb29wMmZ5bjZ2MjZ1NW9x");
            request.AddHeader("Content-Type", "application/json");
            request.AddParameter("undefined", "{\r\n  \"definitionId\": 1,\r\n  \"description\": \"Creating Sample release\",\r\n  \"artifacts\": [\r\n    {\r\n      \"alias\": \"_MyFirstProject\",\r\n      \"instanceReference\": {\r\n        \"id\": \"2\",\r\n        \"name\": null\r\n      }\r\n    }\r\n  ],\r\n  \"isDraft\": false,\r\n  \"reason\": \"none\",\r\n  \"manualEnvironments\": \"PROD\"\r\n}", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);