title: How to make an HTTP Patch request from C# with a parameter [FromBody]
body: <p>I am creating a web API Service but I am having some problems when it comes to calling HTTP Patch requests. Though I know how to create them.
  If you could help me with this I would appreciate it, here is my code:</p>
  <p>HTTP PATCH CODE:</p>
  <pre><code>[HttpPatch(&quot;{username}&quot;)]
          public async Task&lt;ActionResult&gt; Patch(string username, [FromBody] JsonPatchDocument&lt;User&gt; patchDocument)
          { 
              //If no info has been passed, this API call will return badrequest
              if (patchDocument == null)
                  return BadRequest();
  
              var theUser = await connection.GetAUser(username);
              var originalUser = await connection.GetAUser(username);
  
              if (theUser == null)
                  return NotFound();
  
              //Changes specified in the patchdocument are applied to the user
              patchDocument.ApplyTo(theUser, ModelState);
              //check if the patching has been successful or not
              bool isValid = TryValidateModel(theUser);
  
              if (!isValid)
                  return BadRequest(ModelState);
              else
              {
                  bool check = await connection.UpdateUser(originalUser, theUser);
                  if (check != true)
                      return BadRequest();
                  else
                      return Ok();
              }
  
          }
  </code></pre>
  <p>Corelation with SQL Database (Method called from the previous code):</p>
  <pre><code>        public async Task&lt;bool&gt; UpdateUser(User originalUser, User patchedUser)
          {
              SqlCommand sqlCommand = new SqlCommand();
              try
              {
                  if (originalUser.password != patchedUser.password)
                  {
                      //string command = SQL COMMAND
                      SqlParameter param1 = new SqlParameter();
                      param1.ParameterName = &quot;@password&quot;;
                      param1.Value = patchedUser.password;
                      SqlParameter param2 = new SqlParameter();
                      param2.ParameterName = &quot;@username&quot;;
                      param2.Value = patchedUser.username;
  
                      sqlCommand.CommandText = command;
                      sqlCommand.Parameters.Add(param1);
                      sqlCommand.Parameters.Add(param2);
  
                      connection = new DataBaseConnectionService(configuration, sqlCommand);
                      await connection.GetData();
                  }
                  else
                      return true;
                  return true;
              }
              catch (Exception error)
              {
                  return false;
              }
          }
  </code></pre>
  <p>I have tested it on PostMan, and it works perfectly. The problem is that I know how to pass a JsonPatchDocument FromBody on PostMan, but I don't know how to do it using C# (I need this to make the call from an APP).
  I have tried to look for answers regarding this issue, but I have only found information about how to create the API, not about how to call it from C#.
  That is all, if you need any more information I will provide it as soon as I see your request, thank you all so much for your time, I hope everyone has good day.</p>
  <p><strong>EDIT:</strong>
  After diving deeper into this matter, I have found a way that could work, however It returns me a 405 status code (&quot;Method not allowed&quot;):
  This is the code that I use in my C# App</p>
  <pre><code>        public async Task&lt;string&gt; PatchUser(UserModel User_Raw)
          {
              if (client == null)
                  InitializeClient();
  
              try
              {
                  //converts the UserModel to JsonPatchDocument&lt;UserModel&gt;
                  JsonPatchDocument&lt;UserModel&gt; body = new JsonPatchDocument&lt;UserModel&gt;();
                  body.Replace(e = e, User_Raw);
  
                  //Converts the JsonPatchDocument&lt;UserModel&gt; to Json
                  var serializedJsonDocument = JsonConvert.SerializeObject(body);
                  var stringUser = new StringContent(serializedJsonDocument, UnicodeEncoding.UTF8, &quot;application/json&quot;);
  
                  //
                  var request = new HttpRequestMessage(new HttpMethod(&quot;PATCH&quot;), &quot;Here goes the URL&quot;);
                  request.Content = stringUser;
  
                  //response stores the Post result to later ensure that it has been successful
                  var response = await client.SendAsync(request);
                  response.EnsureSuccessStatusCode();
  
                  string HttpResponse = await response.Content.ReadAsStringAsync();
                  return HttpResponse;
              }
              catch (HttpRequestException error)
              {
                  return null;
              }
              catch (Exception error)
              {
                  jsonResult = null;
                  return jsonResult;
              }
          }
  </code></pre>