I am working on communication between API <-> webAPP via HttpClient.
This is my API controller:
[HttpGet, Route("protocols")]
public async Task<ActionResult> GetProtocols()
{
try
{
var result = await _repository.GetProtocols();
return Ok(result);
}
catch(Exception exception)
{
// to do
return BadRequest(exception.Message);
}
}
this is "fired" from website:
var result = await _httpClient.GetAsync("/api/configuration/protocols");
result.EnsureSuccessStatusCode();
Console.WriteLine(result.Content.ToString());
but this is result via swagger:
I dont know why i dont get result body in website, just status.
###UPDATE
This is my code:
var result = await _httpClient.GetAsync("/api/configuration/protocols");
var test = await result.Content.ReadAsStringAsync();
result.EnsureSuccessStatusCode();
Console.WriteLine(result.Content.ToString());
and this is "test" result:
CodePudding user response:
Content isn't just a string, you will want to read the content. If the payload contains JSON or other string data you can:
await result.Content.ReadAsStringAsync()