0

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();
    }
}
5
  • What is the value of requestUri? Commented Apr 18, 2018 at 16:43
  • Can you see in fiddler that the body of your request (content) is a appropriate json string. Commented Apr 18, 2018 at 17:40
  • @levininja requestUri would be 'System.Uri uri = new System.Uri(rmdomain + "projectname" + "/_apis/release/approvals/" + "releaseid" + "?api-version=4.1-preview.3")' Commented Apr 19, 2018 at 9:57
  • Where you have "releaseid" you need to have a variable for the approvalId. Maybe you just need to remove the quotes (if releaseid is the name of a variable that has your approvalId). Commented Apr 19, 2018 at 13:52
  • 1
    I just fixed that and was about to reply and saw your comment @levininja. That was the issue i was passing releaseid instead of approvalid. Marina Liu's note had me thinking on incorrect parameters. Thank you!! Commented Apr 19, 2018 at 16:19

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the "Using ReleaseManagement REST API’s." link that was really helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.