How to delete change request

babasali
Tera Contributor

i have this C# code:


        public void ChangeRequestDelete(string sys_id) {
            this.GetAccessToken();

            using (var client = new HttpClient())
            {
                // #pragma warning disable CS8602 // no way it's null.
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + this.accessToken.access_token);
                Console.WriteLine("Bearer " + this.accessToken.access_token);
                Console.WriteLine($"expiratino: {this.accessToken.expires_in}");
                // #pragma warning restore CS8602

                string url = $"{this.instanceUrl}/api/sn_chg_rest/{this.API_version}/change/{sys_id}";

                Console.WriteLine(url);
                Task<HttpResponseMessage> task = client.DeleteAsync(url);

                task.Wait(TimeSpan.FromSeconds(this.timeout));

                if (task.IsCompletedSuccessfully && task.Result.IsSuccessStatusCode)
                {
                    var response = task.Result.Content.ReadAsStringAsync().Result;
                    Console.WriteLine(response);
                }
                else if (task.IsCompleted)
                {
                    throw new Exception($"Cannot delete ServiceNow ChangeRequest. Result status code: {task.Result.StatusCode}. Reason phrase: {task.Result.ReasonPhrase}");
                }
                else
                {
                    throw new Exception($"Cannot delete ServiceNow ChangeRequest. Sending was not completed within {this.timeout} seconds.");
                }
            }
        }

 

it prints:

Bearer seK8eRYH8XVvhzfaWMteNLius1Bm2Jn8sOWC_toy-WUptgD4Oavm_vqvr3urjlnLyKYg1ocNrizh6WIZxi4dIg
expiratino: 1799
https://dev6XXXX.service-now.com/api/sn_chg_rest/v1/change/5ae177d897503110bab6bf56f053af58
Unhandled exception. System.Exception: Cannot delete ServiceNow ChangeRequest. Result status code: BadRequest. Reason phrase: Bad Request
   at csharp_test_proj.ServiceNowAPI.ChangeRequestDelete(String sys_id) in C:\work\csharp-test-proj\ServiceNowAPI.cs:line 184
   at csharp_test_proj.Program.Main(String[] args) in C:\work\csharp-test-proj\Program.cs:line 21

 

What is wrong with that?
I tried many other APIs.
The change request exist. I followed the REST api guiide.
Please help me!
Thanks!!

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@babasali 

Did you try testing from postman if it works with same user credentials?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader