Sajal Mittal
Sajal Mittal

Reputation: 123

REST API PATH Create WorkItem not working, code exiting with 0

I am using REST API to create a work item using REST API documentation.For this, i need to use Patch request, but this code not working. program exiting with code 0 (0x0).

            HttpClientHandler httpClientHandler = new HttpClientHandler();
            using (HttpClient client = new HttpClient(httpClientHandler))
            {
              var content = "[{'op': 'add','path': '/fields/System.Title', 'value': 'Title' }]";
                client.DefaultRequestHeaders.Accept.Add(
                            new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                string URLTest = "https://MyProject.visualstudio.com/DefaultCollection/ProjectName/_apis/wit/workitems/$Task?api-version=2.0";
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(
                        System.Text.ASCIIEncoding.ASCII.GetBytes(
                            string.Format("{0}:{1}", "*******", "******"))));
                var method = new HttpMethod("PATCH");
                var request = new HttpRequestMessage(method, URLTest)
                {
                    Content = new StringContent(content, Encoding.UTF8,
                        "application/json-patch+json")
                };

                HttpResponseMessage response = await client.SendAsync(request);

Upvotes: 0

Views: 207

Answers (1)

Midhun Mundayadan
Midhun Mundayadan

Reputation: 3182

0 (0x0) This is just debugging message. You can switch that off by right clicking into the output window and uncheck the thread ended message.

build uri using uriBuilder has to work

var uriBuilder = new UriBuilder(URLTest);
uriBuilder.Scheme = "http";

var request = new HttpRequestMessage(method, uriBuilder.Uri)
                {
                    Content = new StringContent(content, Encoding.UTF8,
                        "application/json-patch+json")
                };

Change the asyc method and find the error message

HttpResponseMessage response = client.Send(request);//Now you will get the error message

else use try catch block

       try
       {
              HttpResponseMessage response = await client.SendAsync(request);
       }
       catch (TaskCanceledException e)
       {
           Debug.WriteLine("ERROR: " + e.ToString());
       }

Upvotes: 1

Related Questions