コード例 #1
0
ファイル: VM.cs プロジェクト: Eladzy/FoodAPI
        public VM()

        {
            AccessAPI = new AccessFoodAPI();
            SetCollection();
            ThisCommand = new MyCommand(PostToAPI);
            foodVmObj   = new FoodPoco();
        }
コード例 #2
0
        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);
                }
            }
        }