title: Using HttpClient.GetFromJsonAsync(), how to handle HttpRequestException based on HttpStatusCode without extra SendAsync calls?
body: <p><code>System.Net.Http.Json</code>'s <code>HttpClient</code> extension methods such as <code>GetFromJsonAsync()</code> greatly simplifies the routine codes to retrieve json objects from a web API. It's a pleasure to use.</p>
  <p>But because of the way it's designed (returning deserialized objects directly), it does not produce any <code>HttpResponseMessage</code> for inspection that allows me to take custom actions based on <code>HttpStatusCode</code>.</p>
  <p>Instead, non-success status codes results in a <code>HttpRequestException</code>, which does not appear to offer any properties that expose strongly typed <code>HttpStatusCode</code>. Instead, the status code is included in the exception's <code>Message</code> string itself.</p>
  <p><strong>Edit: .NET 5.0 added the <code>HttpRequestException.StatusCode</code> property, so it can now be inspected when calling <code>GetFromJsonAsync</code>.</strong></p>
  <p>//old post below</p>
  <p>So I've been doing something like this:</p>
  <pre><code>try
  {
    var cars = await httpClient.GetFromJsonAsync&lt;List&lt;Car&gt;&gt;(&quot;/api/cars&quot;);
    //...
  }
              
  catch (HttpRequestException ex)
  {
     if (ex.Message.Contains(HttpStatusCode.Unauthorized.ToString()))
     {
       //Show unauthorized error page...
     }
     //...
  }
  </code></pre>
  <p>This feels a bit hacky. With the old school way of creating <code>HttpRequestMessage</code> and calling <code>SendAsync</code>, we naturally got the chance to inspect a response's <code>HttpResponseMessage.StatusCode</code>. Adding some of those codes back would defeat the convenient purpose of using the one-liners in <code>System.Net.Http.Json</code>.</p>
  <p>Any suggestions here would be greatly appreciated.</p>