Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting JSON data using HttpClient

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
like image 827
Anjitha Avatar asked Oct 31 '22 01:10

Anjitha


1 Answers

Try below code-

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

where RootObject is response object.

like image 105
Sanket Avatar answered Nov 09 '22 03:11

Sanket