How do I do a patch request using HttpClient in dotnet core?

Tom Aalbers picture Tom Aalbers · Oct 2, 2016 · Viewed 11.6k times · Source

I am trying to create a Patch request with theHttpClient in dotnet core. I have found the other methods,

using (var client = new HttpClient())
{
    client.GetAsync("/posts");
    client.PostAsync("/posts", ...);
    client.PutAsync("/posts", ...);
    client.DeleteAsync("/posts");
}

but can't seem to find the Patch option. Is it possible to do a Patch request with the HttpClient? If so, can someone show me an example how to do it?

Answer

Tom Aalbers picture Tom Aalbers · Oct 2, 2016

Thanks to Daniel A. White's comment, I got the following working.

using (var client = new HttpClient())
{       
    var request = new HttpRequestMessage(new HttpMethod("PATCH"), "your-api-endpoint");

    try
    {
        response = await client.SendAsync(request);
    }
    catch (HttpRequestException ex)
    {
        // Failed
    }
}