static async Task <Elephants> AddNewResource(Elephants newElps) { var jsonContent = JsonConvert.SerializeObject(newElps); StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(uri, content); if (response.StatusCode != HttpStatusCode.Conflict) { response.EnsureSuccessStatusCode(); string jsonString = await response.Content.ReadAsStringAsync(); var newlyCreatedResource = JsonConvert.DeserializeObject <Elephants>(jsonString); return(newlyCreatedResource); } else { throw new Exception("Customer already exist, try anther Id"); } }
static async void RunAsync() { client.BaseAddress = new Uri(uri); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Get all data from resources IList <Elephants> getAllList = await GetAllElephantsAsync(); ShowObjectData(getAllList); Console.WriteLine("Get By Id ..............................."); var getAllListById = await GetAllElephantsById("1"); ShowSingleObjectData(getAllListById); Console.WriteLine("Add New Resource ..............................."); Elephants newElephant = new Elephants() { Id = 33, Name = "poly", Species = "South-African", Weight = 10000 }; var newResource = await AddNewResource(newElephant); ShowSingleObjectData(newResource); }
static void ShowSingleObjectData(Elephants elps) { Console.WriteLine(elps.Id + ":" + elps.Name + ":" + elps.Species); }