public VM() { AccessAPI = new AccessFoodAPI(); SetCollection(); ThisCommand = new MyCommand(PostToAPI); foodVmObj = new FoodPoco(); }
public async void Post(FoodPoco foodObj) { string query = "/post"; using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(apiString + query); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = await client.PostAsJsonAsync(client.BaseAddress, foodObj); if (response.IsSuccessStatusCode) { DataList.Add(foodObj); MessageBox.Show("Done", "Success", MessageBoxButton.OK, MessageBoxImage.Asterisk); } else { MessageBox.Show($"{response.StatusCode}, {response.ReasonPhrase}", "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } } }