0

I'm using asp.net MVC 5 to consume API that also developed in asp.net MVC.

For POST and GET requests, I managed to make it work, except for PATCH that always get 400 bad request from web service.

This is what I do in my client controller:

using (HttpClient httpClient1 = new HttpClient())
{
  string apiURLGetClientApproval = "/clients/approvals?action=" + actionType;
  HttpMethod method = new HttpMethod("PATCH");
  HttpRequestMessage message = new HttpRequestMessage(method, new Uri(baseAddress + apiURLGetClientApproval));
  StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
  httpClient1.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token.AccessToken);
  message.Content = content;
  var result = httpClient1.SendAsync(message).Result;
}

This is the content that I pass from my client to API:

{{"clients": [
  {
     "cn": "1132196",
     "hitdate": "04/05/2021"
}]}}

PS :

I access API by postman and ajax from client side with this content, got success reponse.

I have tried with these solution, but same 400 error bad request responsed : PATCH Async requests with Windows.Web.Http.HttpClient class

This is how the parameter of API look like:

    [CustomAuth(Roles = "Super Admin, Admin, User")]
    [HttpPatch]
    [Route("clients/approvals")]
    public HttpResponseMessage UpdateClientApproval(HttpRequestMessage request, string action, [FromBody]JObject data)
    {..... }
Nothing
  • 2,644
  • 11
  • 64
  • 115
  • For the first time in my life I see somebody who is using PATCH. I hope there is another one and he maybe will be able to help you. Good luck! – Serge Jul 25 '21 at 12:28

1 Answers1

0

I have been dealing with the same exact problem for 2 days now. I just fixed it. I realised that sending a PATCH request probably required some specific payload [{"op":"replace"....}] as we can tell from using PostMan. However the PUT request doesn't, in fact most of the data on the business object would already be populated, so you modify what you want to change and send a PUT request instead. I just did that. I had to add the PUT action method in my controller and change the HttpClient to send a PUT request and it worked less than 5mins ago.