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

1 Answer 1

1

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());
       }
Sign up to request clarification or add additional context in comments.

4 Comments

No this is not working :( Issue is after below first line code stops and return 0it does not move ahead.. I guess there is some issue in request. HttpResponseMessage response = await client.SendAsync(request); var Response = response.Content;
In HttpClient there is no Send() method :(
use try catch block and find the TaskCanceledException @SajalMittal
I already used try-catch block, thing is it does not move to catch block either.. :(

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.