Amit Bhisikar
Amit Bhisikar

Reputation: 69

How to Async httpclient with Patch Method

I am trying to consume [this API] (https://learn.microsoft.com/en-us/rest/api/vsts/release/approvals/update). Below is my code, but i am getting 400 bad request.

HttpContent z = new StringContent("{\"status\": \"approved\",\"comments\": \"" + Request.QueryString["comment"].ToString() + "\"}", Encoding.UTF8, "application/json");                    

public static async  Task PatchAsync(Uri requestUri, HttpContent content)
{
    try
    {                 
        using (HttpClient client = new HttpClient())
        {
            var method = new HttpMethod("PATCH");
            var request = new HttpRequestMessage(method, requestUri)
            {
                Content = content
            };

            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}", "", "XXXXXXXXX"))));

            //using (HttpResponseMessage response = await client.PostAsync(requestUri, content))
            using (HttpResponseMessage response = await client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                respApproval = responseBody;
            }
        }
    }
    catch (Exception ex)
    {
        respApproval = ex.ToString();
    }
}

Upvotes: 0

Views: 5780

Answers (1)

Marina Liu
Marina Liu

Reputation: 38126

Since you only provide part of the code, I posted my code (which can update approvals successfully) below for your refernce:

public static async void ApproveRelease()
{
  try
  {
    var username = "alternate auth or PAT";
    var password = "password";
    string accountName = "https://account.visualstudio.com";
    string projectName = "projectname";
    int approvalid = id;
    var approveReleaseUri = "https://accountname.vsrm.visualstudio.com/projectname/_apis/release/approvals/approvlID?api-version=4.1-preview.3";

    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
        Convert.ToBase64String(
          System.Text.ASCIIEncoding.ASCII.GetBytes(
            string.Format("{0}:{1}", username, password))));

        var method = new HttpMethod("PATCH");
        string approvveReleaseMetaData = "{\"status\":\"approved\", \"comments\":\"Good to go\"}";
        var request = new HttpRequestMessage(method, string.Format(approveReleaseUri, accountName, projectName, approvalid, apiVersion))
        {
            Content = new StringContent(approvveReleaseMetaData, Encoding.UTF8, "application/json")
        };

        using (HttpResponseMessage response = client.SendAsync(request).Result)
        {
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
    }
    }
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.ToString());
  }

}

By referring the blog Using ReleaseManagement REST API’s.

Note: you can only update a release approval which status is pending. If you try to update a release approval which approval status is approved or rejected, you will also get the 400 bad request response.

Upvotes: 3

Related Questions