I am developing (at least attempting anyways...) a xamarin android app with connection to API. Below is a basic example of what I am trying to do with a Post. I am not sure how to return the error message (not just code) back to client? I am open to a better way of doing this as long as a complete example can be provided.
API code:
[HttpPost("Consume")]//*
public IActionResult ConsumeInventory([FromBody] Part part)
{
try
{
_repo.ConsumeInventory(part);
}
catch (Exception ex)
{
//ModelState.AddModelError("Error", ex.Message);
return NotFound("Not Enough Inventory");
}
return Ok(part);
}
Client side call:
public async Task<HttpResponseMessage> UpdatePartAsync(Part part, string post_url)
{
string jsonString = JsonConvert.SerializeObject(part);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.PostAsync("invcount/" + post_url, new StringContent(jsonString, Encoding.UTF8, "application/json"));
if (response.StatusCode == HttpStatusCode.NotFound)
{...//Would like to display "Not enough Inventory" in here}
return response;
}