Home > Software design >  after sending GET request only get status response, not response body
after sending GET request only get status response, not response body

Time:11-25

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());

and this is result: enter image description here

but this is result via swagger: enter image description here

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:

enter image description here

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()
  • Related