Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# Post, Put and Patch as JSON async extensions
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));
}
}
}
@jakkritpotter1

This comment has been minimized.

Copy link

@jakkritpotter1 jakkritpotter1 commented Jul 14, 2016

thank you!!

@PurwantoGZ

This comment has been minimized.

Copy link

@PurwantoGZ PurwantoGZ commented Feb 7, 2017

amazing, thx

@mtzaldo

This comment has been minimized.

Copy link

@mtzaldo mtzaldo commented May 26, 2017

thanks n__n

@pawanunique

This comment has been minimized.

Copy link

@pawanunique pawanunique commented Oct 19, 2018

Thanks a lot :) God bless you !

@frodriz

This comment has been minimized.

Copy link

@frodriz frodriz commented Nov 28, 2018

Thanks!

@AmayKulkarni

This comment has been minimized.

Copy link

@AmayKulkarni AmayKulkarni commented Apr 15, 2020

Hi Alex,

i am getting error if i call above method.

Thanks,
Amay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment