I am trying to create a service hook subscription in Visual Studio Team Services, previously Visual Studio Online, programatically. When a project is created in Team Services, service hook will be automatically created. Following is my code for 'workitem created' 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 '',
.
Can any one please help me to solve this?
Thanks in advance.
Stack Trace is:
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
Try below code-
var dataObjects = response1.Content.ReadAsStringAsync().Result;
var rootObj = JsonConvert.DeserializeObject<RootObject>(dataObjects);
where RootObject is response object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With