Search code examples
servicestackservicestack-text

ServiceStack HttpUtils - How to get error body using .NET 6


Is there a way to capture the error message using ServiceStack HttpUtils from my .NET 6 project. Now it returns an empty string...

try
{
    var response = await GetApiUrl().CombineWith("/Controller/DoSomething").PostJsonToUrlAsync(body);
}
catch (Exception ex)
{
    var errorBody = await ex.GetResponseBodyAsync();
}

Solution

  • GetResponseBodyAsync only resolves .NET HttpWebRequest response bodies, it's too late to read the body from the HttpClient exception, you'd need to use the HttpClient directly to be able to read the body from failed responses.