Monojit Sarkar
Monojit Sarkar

Reputation: 2451

WebAPI:How to read message from HttpResponseMessage class at client side

below is a sample action which is returning HttpResponseMessage and if any error occur then this way web api action returning error message and status code to client side return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);.

[HttpGet, Route("GetAll")]
public HttpResponseMessage GetAllCustomers()
{
    IEnumerable<Customer> customers = repository.GetAll();
    if (customers == null)
    {
        var message = string.Format("No customers found");
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
    }
    else
    {
        return Request.CreateResponse(HttpStatusCode.OK, customers);
    }
}

when i am invoking action by http client then i am not getting message in ReasonPhrase property. just tell me what is the right way to read message at client side which is passing like this way return Request.CreateResponse(HttpStatusCode.OK, customers);

here is my client side code

private async void btnFind_Click(object sender, EventArgs e)
{
    var fullAddress = baseAddress + "api/customer/GetByID/"+txtFind.Text;
    Customer _Customer = null;

    using (var client = new HttpClient())
    {
        using (var response = client.GetAsync(fullAddress).Result)
        {
            if (response.IsSuccessStatusCode)
            {
                var customerJsonString = await response.Content.ReadAsStringAsync();
                _Customer = JsonConvert.DeserializeObject<Customer>(customerJsonString);
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
        }
    }

    if (_Customer != null)
    {
        var _CustList = new List<Customer> { _Customer };
        dgCustomers.DataSource = _CustList;
    }

}

response.ReasonPhrase not holding the message which i am passing from action. so may be i am not doing things to read message. please tell me what to change in my code to read the message. thanks

Upvotes: 0

Views: 1994

Answers (1)

Monojit Sarkar
Monojit Sarkar

Reputation: 2451

i have the job this way.

private async void btnFind_Click(object sender, EventArgs e)
{
    var fullAddress = baseAddress + "api/customer/GetByID/"+txtFind.Text;
    Customer _Customer = null;
    try
    {
        using (var client = new HttpClient())
        {
            using (var response = client.GetAsync(fullAddress).Result)
            {
                if (response.IsSuccessStatusCode)
                {
                    var customerJsonString = await response.Content.ReadAsStringAsync();
                    _Customer = JsonConvert.DeserializeObject<Customer>(customerJsonString);
                }
                else
                {
                    //Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                    var ErrMsg = JsonConvert.DeserializeObject<dynamic>(response.Content.ReadAsStringAsync().Result);
                   MessageBox.Show(ErrMsg.Message);
                }
            }
        }

        if (_Customer != null)
        {
            var _CustList = new List<Customer> { _Customer };
            dgCustomers.DataSource = _CustList;
        }
    }
    catch (HttpRequestException ex)
    {
        // catch any exception here
    }
}

read error message this way.

var ErrMsg = JsonConvert.DeserializeObject<dynamic>(response.Content.ReadAsStringAsync().Result);

Upvotes: 1

Related Questions