static void Main(string[] args) { var httpClient = new HttpClient(); httpClient.BaseAddress = new Uri(ServerAddress); // Artists with JSON var artist = new ArtistApiModel() { Name = "Pesho123", Country = "Bulgaria", DateOfBirth = DateTime.Now }; Console.WriteLine("Adding following artist " + artist); Console.WriteLine("-----------------------------------"); Artist.Add(httpClient, artist); Console.ReadLine(); Console.WriteLine("Listing all artists"); Console.WriteLine("-----------------------------------"); Artist.Print(httpClient); Console.ReadLine(); var updatedArtist = new ArtistApiModel() { Name = "PeshoUpdated", Country = "Zimbabve", DateOfBirth = DateTime.Now }; Console.WriteLine("Update artist with ID 1 to " + updatedArtist); Console.WriteLine("-----------------------------------"); Artist.Update(httpClient, 1, artist); Console.ReadLine(); Console.WriteLine("Delete artist with ID 1"); Console.WriteLine("-----------------------------------"); Artist.Delete(httpClient, 1); Console.ReadLine(); // Songs with XML var song = new SongApiModel() { Title = "Nothing else matters", Genre = "Blues", Year = 1995, ArtistId = 1}; Song.Add(httpClient, song); Console.ReadLine(); Console.WriteLine("-----------------------------------"); Song.Print(httpClient); Console.ReadLine(); }
public static async void Update(HttpClient httpClient, int id, ArtistApiModel artist) { HttpContent putContent = new StringContent(JsonConvert.SerializeObject(artist)); putContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); var response = await httpClient.PutAsync("artist/" + id, putContent); try { response.EnsureSuccessStatusCode(); Console.WriteLine("Artist updated!"); } catch (Exception) { Console.WriteLine("Error creating artist"); } }