Thursday, September 18, 2014

How to send an HTTP PATCH Request to a RESTful ODataController Web API Service using Postman

In this post we'll learn Step by step how to send an HTTP PATCH Request to a RESTful ODataController Web API Service using Postman
We'll use Postman to test a RESTful OData Web API application, sending an HTTP PATCH request.
There is an important difference between the two methods for updating entities , HTTP PUT and HTTP PATCH.
An HTTP PUT request is supposed to provide an object to be UPDATED , such object including all of the object's properties , and not only the updated ones , as the HTTP PATCH does . If you need to send a PARTIAL object , you must use the HTTP PATCH method. Therefore , the object send is bound at the Controller to a "Delta<>" local variable , and not to an instance of the object. We'll use here  a working OData Web API  ODataController built in a previous tutorial,   and we'll update an item using  Postman   :

how to send an HTTP PATCH Request to a RESTful  ODataController Web API Service using Postman

In order to setup Postman ,  go to the Chrome Tools >> Extensions   ,   search for "Postman" and install the Application .

HTTP PATCH Request to a RESTful ODataController Web API using Postman

Now let's learn how to call the OData Web API : in MVC , in order to setup  the ODataController , there must be an ODataModelBuilder registered at the "Register" method called from the Global.asax file : as you see in the following snapshot , the EntitySet name MUST be the name of the Controller ( "Notes" in our case ) , so we'll look for a "NotesController" ODataController at the application :

Also , there is a route prefix declared ( "ODataV4" ) , so we'll write it in the URI . 

Now we check for the "Patch" action  , since we're sending an HTTP PATCH request :

Why we check this ? Because we must know which type is the ODataController expecting : in our case , it expects a "Delta<Note>" object , which is a partial generic object included in the request's body ( "[FromBody]" ) . 
Also, we must comply with the declaration inside the Model , respecting the constraints :

We could instead ask for the METADATA to construct the Note object , but that way we won't know the constraints set at the Data Annotations   :


The Port can be seen at the Web tab inside the application properties:

So set the URI , write the Content-Type at the request's Header ,  and select "PATCH" from the list  :

Then , write the JSON object , pushing the "Raw" button :

Important : since the OData protocol is case sensitive , if we type "notes" instead of "Notes" , we will not reach the controller.

Send the request : 

You can see that the Action method got the Delta<> object :

The following is an example of an incomplete partial Note object , containing ONLY 1 property to be updated :

As you see , the response is 200 OK , meaning that the item was successfully updated .

The details of the request can be seen in Postman as follows :

That's all... 
In this tutorial we've learned how to send an HTTP PATCH Request to a RESTful  ODataController Web API Service using Postman.
Happy programming.....
      By Carmel Shvartzman
כתב: כרמל שוורצמן