private async void onSubmit(object sender, EventArgs e) { bool isFieldFilled = IsFieldFilled(); if (isFieldFilled) { string gender = (string)cb_gender.SelectedItem; string name = tb_name.Text; string surname = tb_surname.Text; string email = tb_email.Text; string gsm = tb_gsm.Text; string street = tb_street.Text; string zipcode = tb_zipcode.Text; string city = tb_city.Text; string country = tb_country.Text; Guarantor guarantor = new Guarantor(null, gender, name, surname, email, gsm); guarantor.Address = new Location(street, zipcode, city, country); Dictionary <string, object> guarantorDic = Utils.ToDictionnary(guarantor); string guarantorJSON = Utils.ToJson(guarantorDic); APIRes response = await Utils.Post(@"http://localhost:5000/api/guarantors", guarantorJSON); if (response.Success) { MessageBox.Show(response.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } MessageBox.Show(response.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public static async Task <APIRes> Post(string uri, string body) { HttpContent data = new StringContent(body, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(uri, data); Console.WriteLine("RESPONSE :" + response); string responseString = await response.Content.ReadAsStringAsync(); APIRes entity = JsonConvert.DeserializeObject <APIRes>(responseString); return(entity); }
private async void onSubmit(object sender, EventArgs e) { bool isFieldsFilled = FormUtils.isFieldFilled( tb_name, tb_surname, tb_email, tb_gsm, tb_nationalRegistry, tb_street, tb_zipcode, tb_city, tb_country, cb_guarantor); if (isFieldsFilled) { MessageBox.Show("Chargement ...", "", MessageBoxButtons.OK, MessageBoxIcon.Information); string gender = (string)cb_gender.SelectedItem; string name = tb_name.Text; string surname = tb_surname.Text; string email = tb_email.Text; string gsm = tb_gsm.Text; string nationalRegistry = tb_nationalRegistry.Text; DateTime birthDate = dtp_birthDate.Value; string street = tb_street.Text; string zipcode = tb_zipcode.Text; string city = tb_city.Text; string country = tb_country.Text; Occupant occupant = new Occupant(null, gender, name, surname, email, gsm, nationalRegistry, birthDate, guarantorSelected.ID); occupant.Address = new Location(street, zipcode, city, country); Dictionary <string, object> occupantDic = Utils.ToDictionnary(occupant); string occupantJSON = Utils.ToJson(occupantDic); APIRes response = await Utils.Post(@"http://localhost:5000/api/occupants", occupantJSON); if (response.Success) { MessageBox.Show(response.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } MessageBox.Show(response.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); } }