Sunday, March 1, 2015

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

In this post we'll learn Step by step how to send an HTTP PUT Request to a RESTful ODataController Web API Service using Postman
We'll make use of the Postman tool to test a RESTful OData Web API , sending an HTTP PUT request. That  working OData Web API with ODataController , was built in a previous tutorial. After we update the  record , we check it sending an HTTP GET request :

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



Important : An HTTP PUT request is expected to provide an object to be UPDATED , but the object MUST include all of the object's properties , not just the updated ones . If you want to send a PARTIAL object , you must use the HTTP PATCH verb instead . We explain it in this HTTP PATCH tutorial .

If you do not have Postman installed , just go to the Tools >> Extensions on the Chrome browser , make a search for "Postman" and install the App .

HTTP PUT Request to a RESTful ODataController Web API Service using Postman



Now let's check out how to call the OData Web API : in MVC , to setup  the ODataController there have to be an ODataModelBuilder at the "Register" method called from the Global.asax :  the EntitySet name MUST be called after the name of the Controller ( "Notes" in our case ) , so we'll search for a "NotesController" at the application :

Notice there is a route prefix declared ( "ODataV4" ) , which must be appended to the URI . 



Found the ODataController , now we look for the "Put" method , because we're sending an HTTP PUT request :


Why to check this ? Because we want to know which parameters is expecting the ODataController  :   it expects :
1) a "key" which is the ID of the item : must be in the URI
2) a "Note" object , which must be included in the request's body ( "[FromBody]" ) . 

How do we create a Note object ? Take a look at the declaration inside the Model :



Now we know the Note's properties and its constraints.

Alternatively , you could ask for the METADATA info , but that way you won't know the Data Annotations constraints declared at the Model :

http://localhost:21435/ODataV4/$metadata

The Port  can be obtained from the Web tab at the application properties:


Merge all of this together , and you will have built the URI for the PUT request : "http://localhost:21435/ODataV4/Notes(5)" :

Set the URI , select the PUT method , set the "Content-Type" header , and write the JSON object according to the Note's declaration:


Important : because the OData protocol is case sensitive , if you type "notes" instead of "Notes" , you will not reach the data.


Send the request : as you see , the Controller has bound the JSON object to a local Note variable :




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


 The update was made , and an HTTP GET request can verify that :



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







No comments:

Post a Comment