Adding PATCH support to HttpClient

I'm not going to go into a lot of detail here. Quite simple, the ASP.NET Web API HttpClient doesn't include PATCH support out of the box or rather we don't have nice extension methods for doing so. So here you go:

    public static Task<HttpResponseMessage> PatchAsJsonAsync<T>(this HttpClient client, string requestUri, T value)
        Ensure.Argument.NotNull(client, "client");
        Ensure.Argument.NotNullOrEmpty(requestUri, "requestUri");
        Ensure.Argument.NotNull(value, "value");

        var content = new ObjectContent<T>(value, new JsonMediaTypeFormatter());
        var request = new HttpRequestMessage(new HttpMethod("PATCH"), requestUri) { Content = content };

        return client.SendAsync(request);

If you need to pass a cancellation token or a specific formatter then you can create an overload for that. The above is all I need for now.

Ben Foster

About Me

I'm a software engineer and aspiring entrepreneur with 12+ years experience in the tech industry and have worked with startups and SMB’s in areas such as healthcare, recruitment and e-commerce (I even worked in enterprise, once). I founded my first startup Fabrik in 2011.

I now head up the engineering team at Checkout. If you're interested in working in an exciting and innovative fin-tech company, drop me a message.

Creative Commons Licence