refit icon indicating copy to clipboard operation
refit copied to clipboard

Impossible to read content when HTTP error code is returned

Open andreinitescu opened this issue 3 years ago • 1 comments

Beause of this https://github.com/reactiveui/refit/blob/main/Refit/RequestBuilderImplementation.cs#L272 it's not possible to read the body in case of an HTTP error code

Reading the body is very useful in cases when server returns error codes in the body

andreinitescu avatar Oct 23 '21 07:10 andreinitescu

You could probably use something like this assuming you have a type for the error to deserialise into

/// <summary>
/// Get content from response or error.
/// </summary>
/// <typeparam name="T">Type to convert.</typeparam>
/// <param name="response">The response.</param>
/// <returns>T:from content.</returns>
public static async Task<T> GetContentAsync<T>(this ApiResponse<T> response)
{
    _ = response ?? throw new ArgumentNullException(nameof(response));
    return response.Content ?? await response.Error.GetContentAsAsync<T>();
}

4nandP avatar Dec 17 '21 06:12 4nandP