1

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

Joe Clinton
  • 145
  • 1
  • 12

1 Answers1

4

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);
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • I tried by putting a json in the StringContent like - `Content = new StringContent("[{ \"op\": \"replace\", \"path\": \"Name\", \"value\": \"Ram\"},{ \"op\": \"replace\", \"path\": \"StartLocation\", \"value\": \"Moscow\"}]", Encoding.UTF8, "application /json-patch+json")` but I get the run time error - 'The format of value 'application /json-patch+json' is invalid.' – Joe Clinton Sep 18 '18 at 10:47
  • So i changed the 3rd paramter to 'application/json' - `Content = new StringContent("[{ \"op\": \"replace\", \"path\": \"Name\", \"value\": \"Ram\"},{ \"op\": \"replace\", \"path\": \"StartLocation\", \"value\": \"Moscow\"}]", Encoding.UTF8, "application/json")` and it worked perfectly, thank you Chris. – Joe Clinton Sep 18 '18 at 10:56