diff --git a/BinaryDad.Extensions/Extensions/HttpExtensions.cs b/BinaryDad.Extensions/Extensions/HttpExtensions.cs new file mode 100644 index 0000000..30c7645 --- /dev/null +++ b/BinaryDad.Extensions/Extensions/HttpExtensions.cs @@ -0,0 +1,39 @@ +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace BinaryDad.Extensions +{ + public static class HttpExtensions + { + /// + /// Send a POST request to the specified Uri as an asynchronous operation. + /// + /// + /// + /// + /// + /// Specifies whether to serialize using for WCF/REST methods + /// + public static Task PostAsJsonAsync(this HttpClient client, string requestUrl, object model, bool useDataContractJsonSerializer = false) + { + var content = new StringContent(model.Serialize(), Encoding.UTF8, "application/json"); + + return client.PostAsync(requestUrl, content); + } + + /// + /// Serialize the HTTP content to type as an asynchronous operation. + /// + /// + /// + /// Specifies whether to deserialize using for WCF/REST methods + /// + public static async Task ReadAsAsync(this HttpContent httpContent, bool useDataContractJsonSerializer = false) + { + var result = await httpContent.ReadAsStringAsync(); + + return result.Deserialize(); + } + } +}