-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathHttpClientExtensions.cs
More file actions
36 lines (28 loc) · 964 Bytes
/
HttpClientExtensions.cs
File metadata and controls
36 lines (28 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
namespace AuthenticationWithClientSideBlazor.Client
{
public static class HttpClientExtensions
{
public static async Task<T> ReadAsJsonAsync<T>(this HttpContent content)
{
var dataAsString = "";
try
{
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
dataAsString = await content.ReadAsStringAsync();
if (string.IsNullOrEmpty(dataAsString)) return default;
return JsonSerializer.Deserialize<T>(dataAsString, options);
}
catch (Exception e)
{
throw new ArgumentException($"Invalid json: '{dataAsString}'", e.InnerException);
}
}
}
}