JaimeSantos
JaimeSantos

Reputation: 23

How to make an HTTP Patch request from C# with a parameter [FromBody]

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:

HTTP PATCH CODE:

[HttpPatch("{username}")]
        public async Task<ActionResult> Patch(string username, [FromBody] JsonPatchDocument<User> 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();
            }

        }

Corelation with SQL Database (Method called from the previous code):

        public async Task<bool> 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 = "@password";
                    param1.Value = patchedUser.password;
                    SqlParameter param2 = new SqlParameter();
                    param2.ParameterName = "@username";
                    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;
            }
        }

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.

EDIT: After diving deeper into this matter, I have found a way that could work, however It returns me a 405 status code ("Method not allowed"): This is the code that I use in my C# App

        public async Task<string> PatchUser(UserModel User_Raw)
        {
            if (client == null)
                InitializeClient();

            try
            {
                //converts the UserModel to JsonPatchDocument<UserModel>
                JsonPatchDocument<UserModel> body = new JsonPatchDocument<UserModel>();
                body.Replace(e = e, User_Raw);

                //Converts the JsonPatchDocument<UserModel> to Json
                var serializedJsonDocument = JsonConvert.SerializeObject(body);
                var stringUser = new StringContent(serializedJsonDocument, UnicodeEncoding.UTF8, "application/json");

                //
                var request = new HttpRequestMessage(new HttpMethod("PATCH"), "Here goes the URL");
                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;
            }
        }

Upvotes: 2

Views: 18276

Answers (1)

Mo B.
Mo B.

Reputation: 5827

Are you asking how to make a PATCH request from a C# application? In that case, why are you showing all the irrelevant server code?

To make a PATCH request, you instantiate an HttpClient, and then do this:

    JsonPatchDocument<User> body = <...>;
    HttpResponseMessage response = await client.PatchAsync(requestUri, body);

If you're stuck with .NET Framework, you won't have PatchAsync, in which case you have to do something like this:

    var request = new HttpRequestMessage(new HttpMethod("PATCH"), requestUri) {
        Content = body
    };
    var response = await client.SendAsync(request);

Upvotes: 8

Related Questions