Forked from alexandrevicenzi/HttpClientExtensions.cs
Created
October 5, 2017 00:44
-
-
Save skynode/8dcbf7ec1e273166041b475b6cc64d93 to your computer and use it in GitHub Desktop.
C# Post, Put and Patch as JSON async extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Net.Http.Formatting; | |
using System.Threading.Tasks; | |
namespace MyProject.Extensions | |
{ | |
public static class HttpClientEx | |
{ | |
public const string MimeJson = "application/json"; | |
public static Task<HttpResponseMessage> PatchAsync(this HttpClient client, string requestUri, HttpContent content) | |
{ | |
HttpRequestMessage request = new HttpRequestMessage | |
{ | |
Method = new HttpMethod("PATCH"), | |
RequestUri = new Uri(client.BaseAddress + requestUri), | |
Content = content, | |
}; | |
return client.SendAsync(request); | |
} | |
public static Task<HttpResponseMessage> PostJsonAsync(this HttpClient client, string requestUri, Type type, object value) | |
{ | |
return client.PostAsync(requestUri, new ObjectContent(type, value, new JsonMediaTypeFormatter(), MimeJson)); | |
} | |
public static Task<HttpResponseMessage> PutJsonAsync(this HttpClient client, string requestUri, Type type, object value) | |
{ | |
return client.PutAsync(requestUri, new ObjectContent(type, value, new JsonMediaTypeFormatter(), MimeJson)); | |
} | |
public static Task<HttpResponseMessage> PatchJsonAsync(this HttpClient client, string requestUri, Type type, object value) | |
{ | |
return client.PatchAsync(requestUri, new ObjectContent(type, value, new JsonMediaTypeFormatter(), MimeJson)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment