locked
Post Async with one or two string type parameter RRS feed

  • Question

  • User264732274 posted

    see my action

            [HttpPost, Route("DeleteCustomer")]
            public HttpResponseMessage DeleteProduct(string customerID)
            {
                HttpResponseMessage response = null;
                Customer customer = repository.Get(customerID);
                if (customer == null)
                {
                    var message = string.Format("No customer found by the ID {0}", customerID);
                    HttpError err = new HttpError(message);
                    response = Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, err);
                    response.ReasonPhrase = message;
                }
                else
                {
                    if(repository.Remove(customerID))
                    {
                        response = Request.CreateResponse<Customer>(HttpStatusCode.Created, customer);
                        response.ReasonPhrase = "Customer successfully deleted";
                    }
                    else
                    {
                        var message = string.Format("Due to some error customer not removed");
                        HttpError err = new HttpError(message);
                        response = Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, err);
                        response.ReasonPhrase = message;
                    }
                }
                
    
                return response;
            }

    now i am not being able to understand how to call it by httpclient. looking for sample code. thanks

    Thursday, September 1, 2016 12:46 PM

Answers

  • User264732274 posted

    call action this way and it worked but not sure right approach has been taken ? thanks

            private void btnDelete_Click(object sender, EventArgs e)
            {
                var uri = new Uri(ConfigurationManager.AppSettings["baseAddress"] + "/api/customer/DeleteCustomer?customerID=CUS01");
    
                try
                {
                    using (var client = new HttpClient())
                    {
                        using (var response = client.PostAsync(uri, new StringContent("")).Result)
                        {
                            if (response.IsSuccessStatusCode)
                            {
                                MessageBox.Show(response.ReasonPhrase);
                            }
                            else
                            {
                                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                                var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);
                                MessageBox.Show(dict["Message"]);
                            }
                        }
                    }
                }
                catch (HttpRequestException ex)
                {
                    // catch any exception here
                }            
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2016 12:57 PM

All replies

  • User264732274 posted

    call action this way and it worked but not sure right approach has been taken ? thanks

            private void btnDelete_Click(object sender, EventArgs e)
            {
                var uri = new Uri(ConfigurationManager.AppSettings["baseAddress"] + "/api/customer/DeleteCustomer?customerID=CUS01");
    
                try
                {
                    using (var client = new HttpClient())
                    {
                        using (var response = client.PostAsync(uri, new StringContent("")).Result)
                        {
                            if (response.IsSuccessStatusCode)
                            {
                                MessageBox.Show(response.ReasonPhrase);
                            }
                            else
                            {
                                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                                var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);
                                MessageBox.Show(dict["Message"]);
                            }
                        }
                    }
                }
                catch (HttpRequestException ex)
                {
                    // catch any exception here
                }            
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2016 12:57 PM
  • User36583972 posted

    Hi sudip_inn,

    You can use it.

    Best Regards,

    Yohann Lu

    Friday, September 2, 2016 2:35 AM