How to invoke the API action of type [HttpPatch] from HttpClient class in asp.net core mvc

823 Views Asked by At

My API has an [HttpPatch] action which i need to invoke.

[HttpPatch("{id}")]
public StatusCodeResult Patch(int id, [FromBody]JsonPatchDocument<Reservation> patch)
{
    Reservation res = Get(id);
    if (res != null)
    {
        patch.ApplyTo(res);
        return Ok();
    }
    return NotFound();
}

I am trying it from HttpClient class but it does not have .PatchAsync() method?

Also the parameter is of type JsonPatchDocument<Reservation> and so how to send it from client when invoking this action?

Please help

1

There are 1 best solutions below

2
On BEST ANSWER

You have to create an HttpRequestMessage manually and send it via SendAsync:

var request = new HttpRequestMessage
{
    RequestUri = new Uri("http://foo.com/api/foo"),
    Method = new HttpMethod("patch"),
    Content = new StringContent(json, Encoding.UTF8, "application/json-patch+json")
};
var response = await _client.SendAsync(request);