Unable to call Patch method of Web API in ASP.NET Core

1.4k Views Asked by At

My API has a method of type Patch that I need to call from the client. This method is:

[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 to call it from my client whose code is:

[HttpPost]
public async Task<IActionResult> UpdateReservationPatch(int id, Reservation reservation)
 {
     using (var httpClient = new HttpClient())
     {
         var request = new HttpRequestMessage
         {
             RequestUri = new Uri("http://localhost:8888/api/Reservation/" + id),
             Method = new HttpMethod("Patch"),
             Content = new StringContent("[{ \"op\":\"replace\", \"path\":\"Name\", \"value\":\"" + reservation.Name + "\"},{ \"op\":\"replace\", \"path\":\"StartLocation\", \"value\":\"" + reservation.StartLocation + "\"}]", Encoding.UTF8, "application/json")
         };

         var response = await httpClient.SendAsync(request);
     }
     return RedirectToAction("Index");
 }

I am failing to do so and getting error.

StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers: { Server: Microsoft-IIS/10.0 X-Powered-By: ASP.NET Date: Tue, 28 Jul 2020 12:25:24 GMT Content-Type: application/problem+json; charset=utf-8 Content-Length: 370 }}

What can be the problem?

0

There are 0 best solutions below