Posting JSON Data Using HttpClient

I am trying to create a service subscription in Visual Studio Team Services, previously Visual Studio Online, programmatically. When a project is created in Team Services, a service hook is automatically created. Below is my code for the created "workitem" web hook:

using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String( System.Text.ASCIIEncoding.ASCII.GetBytes( string.Format("{0}:{1}", username, password)))); var request = new { publisherId = "tfs", eventType= " workitem.created", resourceVersion= "1.0", consumerId= "webHooks", consumerActionId= "httpRequest", publisherInputs= new { projectId= "test123", }, consumerInputs= new { url = "https://mydomain/api/ServiceHook/SaveWorkItem" } }; var response = client.PostAsync("https://mydomain/DefaultCollection/_apis/hooks/subscriptions", new StringContent(JsonConvert.SerializeObject(request).ToString(), Encoding.UTF8, "application/json")) .Result; if (response.IsSuccessStatusCode) { dynamic content = JsonConvert.DeserializeObject( response.Content.ReadAsStringAsync() .Result); // Access variables from the returned JSON object var appHref = content.links.applications.href; } } 

while running this code I got the following error: Unexpected character encountered while parsing value: <. Path '', Unexpected character encountered while parsing value: <. Path '', .

Can someone help me solve this?
Thank you in advance. Stack trace:

 at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.JsonTextReader.Read() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value) at TestApplication.Program.<SetServiceHook>d__1.MoveNext() in C:\Users\Administrator\Documents\Visual Studio 2015\Projects\TestApplication\TestApplication\Program.cs:line 94 
+5
source share
1 answer

Try entering the code -

 var dataObjects = response1.Content.ReadAsStringAsync().Result; var rootObj = JsonConvert.DeserializeObject<RootObject>(dataObjects); 

where RootObject is the response object.

+2
source

All Articles