I had two previous tutorials that talked about Http Patch: 1) how to implement Http Patch method in a web API, and 2) how to create a REST client SPA in Blazor. But sending a Patch request was not the main subject of those tutorials, and people looking for that topic seem to miss it because it doesn’t come up in searches. So, I’m doing something about it!
You can say this is a repetition of what I already talked about in those tutorials, and you would be correct! But, because I don’t have a specific blog post about this, I thought I’d write one!
So, today, I will specifically talk about how to send Http Patch request programmatically using C# using HttpClient
. Also, take note the sample C# code you will see below is in the context of Blazor WASM, because this is taken from my previous Blazor tutorial.
Secondly, I will briefly show how to send the same request using a third-party client. Let’s get started!
The JSON Body
First off, this is the JSON data that you need to send as part of the Patch request:
[
{
"op": "replace",
"path": "LName",
"value": "Smith"
}
]
Here, you want the operation to be "replace"
. Then, you are replacing the field "LName"
, and you want the new value to be "Smith"
.
Sending a Patch request in C#
Sending the request is straightforward. First, you need to package the JSON
data(shown above) in a formatted text like StringContent
object, and then send it via PatchAsync
method call, as shown below:
@inject HttpClient httpClient
private async Task HandleValidSubmit()
{
var stringContent = new StringContent(jsonData, Encoding.UTF8, "application/json");
responseMessage = await httpClient.PatchAsync(ApiUrl, stringContent);
}
This is a simplified version of my previous tutorial, but basically, you inject HttpClient
on line 1. Then, on line 5 you create a StringContent
object with your JSON
data. Finally, on line 7 you call HttpClient.PatchAsync
method and pass it the URL of the API service, and the StringContent
object.
And that is pretty much it! Again you can go back to my tutorial how to create a REST client SPA in Blazor to see the complete tutorial and complete source code. In that tutorial I also showed how to extract the headers from the HttpResponseMessage
object.
Sending a Patch request using a 3rd-party client
For the 3rd-party client, I use a Google Chrome plugin called Boomerang for testing all my REST API applications. Again, it is very simple to use, just enter the URL of your API, then enter you JSON
data in the box, as shown below:
Basically, you have three areas of interest in the interface. There is the top part where you enter the URL, making sure to specify the employee ID as part of the URL, in this example, it is 3
.
The left panel is where you enter the JSON
data.
When you hit the Send button on top, then on the right panel you will get the response back. The Body tab displays the database record that was modified – the API service that I used was programmed to return this info, but your API might opt to not return back the modified record, it really depends on your organization.
Then, finally, on the top of the right panel also, you get the status of the operation, whether it is successful or not, in this example, it is 200 OK which means the operation is successful.
And that is it, folks! Just a little tip on how to send a Patch request in C#, and also how to send a request using a 3rd-party client.
Conclusion
You can send a HTTP PATCH request in Blazor using HttpClient
class of .NET. All you need to do is inject the HttpClient
service into your component.