{"id":124,"date":"2018-03-22T23:22:00","date_gmt":"2018-03-22T22:22:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2018\/03\/22\/restful-web-services-tutorial-3-creating-and-calling-a-post-put-and-delete-methods\/"},"modified":"2020-11-05T14:13:51","modified_gmt":"2020-11-05T13:13:51","slug":"restful-web-services-tutorial-3-creating-and-calling-a-post-put-and-delete-methods","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/restful-web-services-tutorial-3-creating-and-calling-a-post-put-and-delete-methods\/","title":{"rendered":"RESTful Web Services Tutorial 3 &#8211; Creating and Calling a POST, PUT and DELETE Methods"},"content":{"rendered":"<div style=\"color: #555555; font-size: 18px; line-height: 30px; text-align: justify;\">\n<div style=\"font-family: 'segoe ui';\"> In this tutorial, we would focus on creating the POST method, sending POST request and retrieving a response.<\/p>\n<p><b>Content<\/b><\/p>\n<ol>\n<li><a href=\"http:\/\/kindsonthegenius.blogspot.com\/#t1\">Review of <a href=\"http:\/\/kindsonthegenius.blogspot.hu\/2018\/03\/restful-web-services-tutorial-1.html\" target=\"_blank\" rel=\"noopener noreferrer\">Tutorials 1<\/a> <a href=\"https:\/\/kindsonthegenius.com\/blog\/restful-web-services-tutorial-2-setting-up-a-rest-client\/\" target=\"_blank\" rel=\"noopener noreferrer\">and 2<\/a> <\/a><\/li>\n<li><a href=\"http:\/\/kindsonthegenius.blogspot.com\/#t2\">Build a New&nbsp; REST API in Visual Studio 2017<\/a><\/li>\n<li><a href=\"http:\/\/kindsonthegenius.blogspot.com\/#t3\">Understanding the Controller File in Web API Application<\/a><\/li>\n<li><a href=\"http:\/\/kindsonthegenius.blogspot.com\/#t4\">Create the Employee Model<\/a><\/li>\n<li><a href=\"http:\/\/kindsonthegenius.blogspot.com\/#t5\">Create the Employee Controller<\/a> <\/li>\n<li><a href=\"http:\/\/kindsonthegenius.blogspot.com\/#t6\">Set up the GET methods<\/a><\/li>\n<li><a href=\"http:\/\/kindsonthegenius.blogspot.com\/#t7\">Write the POST method<\/a><\/li>\n<li><a href=\"http:\/\/kindsonthegenius.blogspot.com\/#t8\">Write the PUT and DELETE Methods<\/a><\/li>\n<li><a href=\"http:\/\/kindsonthegenius.blogspot.com\/#t9\">Next Steps<\/a><\/li>\n<\/ol>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/1.bp.blogspot.com\/-6yBKCvvtSYQ\/WrQ4pN1i5II\/AAAAAAAABfw\/mDm819izIO0vHehqcwM49Hrp1aUA2Y6PQCLcBGAs\/s1600\/Tutorial%2B3.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"821\" data-original-width=\"1257\" height=\"209\" src=\"https:\/\/1.bp.blogspot.com\/-6yBKCvvtSYQ\/WrQ4pN1i5II\/AAAAAAAABfw\/mDm819izIO0vHehqcwM49Hrp1aUA2Y6PQCLcBGAs\/s320\/Tutorial%2B3.jpg\" width=\"320\" \/><\/a><\/div>\n<p> <ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins><\/p>\n<p><b><\/p>\n<h3>1. Review of <a href=\"https:\/\/kindsonthegenius.com\/blog\/restful-web-services-tutorial-1-creating-a-rest-api-in-visual-studio-net-c\/\" target=\"_blank\" rel=\"noopener noreferrer\">Tutorials 1<\/a> and 2&nbsp;<\/h3>\n<p><\/b><br \/>In Tutorial 1, we build a REST API in Visual Studio and we created an Employee Model. We also added an EmployeeController and wrote GET methods to return list of employees and another GET method to return an employee based on the ID<br \/>In Tutorial 2, we set up a REST Client (Advanced REST Client) and made tested our API.<br \/>In this Tutorial, we will create a new REST API and this time we will writh all the methods for GET, POST, PUT and DELETE operations. <\/p>\n<p><b><\/p>\n<h3>2. Build a New&nbsp; REST API in Visual Studio 2017<\/h3>\n<p><\/b><br \/>This time in Visual Studio&nbsp; 2017 (you can download it for free from this <a href=\"https:\/\/www.visualstudio.com\/downloads\/\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">link<\/a>)<\/p>\n<p>Start a new project to display the New Project Wizard as shown in Figure 1<br \/>Select Web fromt he list of templates on the left <\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/3.bp.blogspot.com\/-vcFHBkMHOC0\/WrPLjtBRoII\/AAAAAAAABeQ\/DW4Ce7k9HR85gsnQYBVGofmnKKRQRqdOACLcBGAs\/s1600\/Figure%2B1.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"723\" data-original-width=\"1018\" height=\"283\" src=\"https:\/\/3.bp.blogspot.com\/-vcFHBkMHOC0\/WrPLjtBRoII\/AAAAAAAABeQ\/DW4Ce7k9HR85gsnQYBVGofmnKKRQRqdOACLcBGAs\/s400\/Figure%2B1.jpg\" width=\"400\" \/>&nbsp;<\/a><\/div>\n<div style=\"clear: both; text-align: center;\">Figure 1 <\/div>\n<p>Give the Project a name (here I use Tutorial3)<br \/>Select ASP.NET Core Web Application<br \/>Click OK. The New AST.NET Core Web Applition templates is displayed as shown in Figure 2<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/1.bp.blogspot.com\/-baOlJHPIigk\/WrPLzxp4yyI\/AAAAAAAABeU\/1npWvhTYBKIr6PgKtEFxOxkm2hCQVCBIACLcBGAs\/s1600\/Figure%2B2.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"583\" data-original-width=\"863\" height=\"270\" src=\"https:\/\/1.bp.blogspot.com\/-baOlJHPIigk\/WrPLzxp4yyI\/AAAAAAAABeU\/1npWvhTYBKIr6PgKtEFxOxkm2hCQVCBIACLcBGAs\/s400\/Figure%2B2.jpg\" width=\"400\" \/><\/a><\/div>\n<div style=\"clear: both; text-align: center;\">Figure 2:&nbsp; <\/div>\n<p>Select .NET Framework in the first box at the upper left if it is not already selecte<br \/>Select ASP.NET Core 2.0 (or 1.1 depending on your version of Visual Studio)<br \/>Click OK. The API application file and folders are generated for you.<\/p>\n<p>Take a look at the folder structure as shown in Figure 3.<br \/>Note that in this case, we no longer have &#8216;WebAPIConfig.cs&#8217; file<\/p>\n<p><b>Quiz<\/b>: Do you remember the configuration we made in teh WebAPIConfig file in Tutorial 1?<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/4.bp.blogspot.com\/-6zK7n1EvkTA\/WrPN7F2rSuI\/AAAAAAAABek\/qLR7ReDTLiQxOEgCkXi3IC1igZraVq5sQCPcBGAYYCw\/s1600\/Figure%2B3.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"929\" data-original-width=\"737\" height=\"400\" src=\"https:\/\/4.bp.blogspot.com\/-6zK7n1EvkTA\/WrPN7F2rSuI\/AAAAAAAABek\/qLR7ReDTLiQxOEgCkXi3IC1igZraVq5sQCPcBGAYYCw\/s400\/Figure%2B3.jpg\" width=\"316\" \/><\/a><\/div>\n<p><\/p>\n<div style=\"clear: both; text-align: center;\">Figure 3: Folder Structure <\/div>\n<p>Note that there is a controller called ValuesController in the Controllers folder<br \/>Open the ValuesController and examine the content and check that it is as shown in Figure 6<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/2.bp.blogspot.com\/-0X-y1TD1Cdk\/WrPObJAxbAI\/AAAAAAAABeo\/NMWZ6TYG6dIlNNR_1OiRrsf3c7nbV_gbACEwYBhgL\/s1600\/Figure%2B4%2B-%2BValuesController%2BFile.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"1034\" data-original-width=\"1112\" height=\"594\" src=\"https:\/\/2.bp.blogspot.com\/-0X-y1TD1Cdk\/WrPObJAxbAI\/AAAAAAAABeo\/NMWZ6TYG6dIlNNR_1OiRrsf3c7nbV_gbACEwYBhgL\/s640\/Figure%2B4%2B-%2BValuesController%2BFile.jpg\" width=\"640\" \/><\/a><\/div>\n<div style=\"clear: both; text-align: center;\">Figure 4: ValuesController File <\/div>\n<p>Now, Run the application and see that the output depends on the first method in the ValuesController File as shown in Listing 1<\/p>\n<p>The controller determines what happesn when a HTTP request is made to the API <br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #0000cc;\">    [Route(\"api\/[controller]<\/span><span style=\"background-color: #fff0f0;\">\")]<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ValuesController<\/span> : Controller<br \/>    {<br \/>        <span style=\"color: #888888;\">\/\/ GET api\/values<\/span><br \/><span style=\"color: #0000cc;\">        [HttpGet]<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">public<\/span> IEnumerable&lt;<span style=\"color: #333399; font-weight: bold;\">string<\/span>&gt; Get()<br \/>        {<br \/>            <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span>[] { <span style=\"background-color: #fff0f0;\">\"value1\"<\/span>, <span style=\"background-color: #fff0f0;\">\"value2\"<\/span> };<br \/>        }<br \/><\/pre>\n<\/div>\n<p>Listing 1: Default Controller file<\/p>\n<p> <ins data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\" style=\"display: block; text-align: center;\"><\/ins><br \/><b><\/p>\n<h3>3. Understanding the Controller File in WEB API Application<\/h3>\n<p><\/b><br \/>The class is annotated with the Route annotation which comes befor the name of the class<br \/>The Route annotation specifies the URL strucuture of the class. In this case the path is given by<\/p>\n<div style=\"text-align: center;\"><span style=\"color: blue;\">api\/[controller]<\/span><\/div>\n<p>This is what comes after the base url. So if the base URL is is<\/p>\n<div style=\"text-align: center;\"><span style=\"color: blue;\">http:\/\/localhost:8000<\/span><\/div>\n<p>Then the url to type to get to this controller would be:<\/p>\n<div style=\"text-align: center;\"><span style=\"color: blue;\">http:\/\/localhost:8080\/api\/Values<\/span><\/div>\n<p>Now inside the controller file, a decision is made on what method is to be executed.<br \/>In the case of the ValuesController, we have one method called <span style=\"color: blue;\">Get(), <\/span>which is annotated with<br \/><span style=\"color: blue;\">[HttpGet]<\/span><\/p>\n<p>This means that a <span style=\"color: blue;\">GET <\/span>request to this controller would execute this method.<br \/>However if there are more than one methods annotated with the [HttpGet] annotation, then an error would be generated.<br \/>To solve this problem, we need to modify the Route annotation of this controller to also contain the name of the method.<\/p>\n<p>So we would write it as:<br \/><span style=\"color: blue;\">api\/[controller]\/[action]<\/span><\/p>\n<p>So in this case, to execute the Get() method, the Request URL and path would be something like:<\/p>\n<p><span style=\"color: blue;\">http:\/\/localhost:8000\/api\/Values\/Get<\/span><\/p>\n<p>REST API also supports function overloading. So we can have another Get() method in this controller, but it must have a different signature (maybe would take a parameter)<\/p>\n<p><b><\/p>\n<h3>4. Create the Employee Model<\/h3>\n<p><\/b> <br \/>You already know that a model is simply a class used to define objects in our API application. To create the Employee model: <\/p>\n<ul>\n<li>Right-click on your project in the solution explorer and add a new Folder<\/li>\n<li>Name it Models<\/li>\n<li>Create a&nbsp; class inside this folder, name the file Employee. This is our employee model(you already know how to do this from Tutorial 1)<\/li>\n<li>Create the employee class using the following code in Listing 2: <\/li>\n<\/ul>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">namespace<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">Tutorial3.Models<\/span><br \/>{<br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Employee<\/span><br \/>    {<br \/>        <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> Id { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }<br \/>        <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span> Firsname { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }<br \/>        <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span> Lastname { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }<br \/>        <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span> Nationality { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }<br \/>        <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span> Phone { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }<br \/>        <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">double<\/span> Score { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }<br \/>    }<br \/>}<br \/><\/pre>\n<\/div>\n<p>Listing 2: Employee Model\/Class<\/p>\n<p><b><\/p>\n<h3>5. Create the Employee Controller<\/h3>\n<p><\/b><br \/>Follow the steps below to add a new controller to the API <\/p>\n<ul>\n<li>Right-click on the Controller folder and Choose Add, then choose Controller&nbsp;<\/li>\n<li>In the New Controller Wizard, Select Empty API Controller<\/li>\n<\/ul>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/3.bp.blogspot.com\/-5i_q_IIIw9M\/WrQpN95I8lI\/AAAAAAAABfg\/G_hGtRgUIJ4u_76ea-6W3j_ELcwGaVw8ACLcBGAs\/s1600\/API%2BController%2B-%2BEmpty.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"723\" data-original-width=\"1018\" height=\"283\" src=\"https:\/\/3.bp.blogspot.com\/-5i_q_IIIw9M\/WrQpN95I8lI\/AAAAAAAABfg\/G_hGtRgUIJ4u_76ea-6W3j_ELcwGaVw8ACLcBGAs\/s400\/API%2BController%2B-%2BEmpty.jpg\" width=\"400\" \/>&nbsp;<\/a><\/div>\n<div style=\"clear: both; text-align: center;\">Figure 5: New Controller Wizzard(Scaffold) <\/div>\n<p><\/p>\n<ul>\n<li>A new controller will be added. <\/li>\n<li>Open it and ensure that the content is as shown in Figure 5<\/li>\n<\/ul>\n<p>Now we would add all our methods (methods are known as actions in a REST API)<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/4.bp.blogspot.com\/-zWUH2XcpFDg\/WrPPjz0g4VI\/AAAAAAAABe0\/1dXQYBia4tAlp5Nq0Aeml2hkZDbv4gm5QCLcBGAs\/s1600\/Figure%2B5%2B-%2BEmpty%2BEmployee%2BController.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"910\" data-original-width=\"1093\" height=\"532\" src=\"https:\/\/4.bp.blogspot.com\/-zWUH2XcpFDg\/WrPPjz0g4VI\/AAAAAAAABe0\/1dXQYBia4tAlp5Nq0Aeml2hkZDbv4gm5QCLcBGAs\/s640\/Figure%2B5%2B-%2BEmpty%2BEmployee%2BController.jpg\" width=\"640\" \/><\/a><\/div>\n<div style=\"clear: both; text-align: center;\">Figure 5: Empty Employee Controller File <\/div>\n<p><b><\/p>\n<h3>6. Create up the GET Methods<\/h3>\n<p><\/b><br \/>Pay attention to this section. In Tutorial 1, we wrote two methods<\/p>\n<ul>\n<li>GetEmployees() and<\/li>\n<li>GetEmployeesByID()<\/li>\n<\/ul>\n<p>But using GetEmployeeByID does not well conform to the RESTful style of resource-based URIs.<br \/>So in this case we would write the the second method also as GetEmployees but we would make is take a parameter with would be the Id of the employee.<br \/>Clear enough? Sure!<\/p>\n<p>The code for the GetEmployees method is given in Listing 3. This method returns all the employees<\/p>\n<p>Copy and paste this code in your EmployeeController file <br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<pre style=\"line-height: 125%; margin: 0;\">        <span style=\"color: #888888;\">\/\/METHOD TO GET ALL EMPLOYEES<\/span><br \/><span style=\"color: #0000cc;\">        [HttpGet]<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">public<\/span> IEnumerable&lt;Employee&gt; GetEmployees()<br \/>        {<br \/>            <span style=\"color: #008800; font-weight: bold;\">return<\/span> employees;<br \/>        }<br \/><\/pre>\n<\/div>\n<p>Listing 3: GetEmployees() Method to return all Employees<\/p>\n<p>Now the next code below is the GetEmployees method that takes a parameter the Id of the employee to be returned.<\/p>\n<p><span style=\"color: red;\">Notice two things:<\/span><\/p>\n<ul>\n<li>the Id parameter is specified as part of the HttpGet annotation<\/li>\n<li>the Id parameter is also specified as a parameter to the GetEmployees method<\/li>\n<\/ul>\n<p>this differentiates it from the previous GetEmployees() method<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<pre style=\"line-height: 125%; margin: 0;\">        <span style=\"color: #888888;\">\/\/METHOD TO GET AND EMPLOYEE BASED ON AN EMPLOYEE ID<\/span><br \/><span style=\"color: #0000cc;\">        [HttpGet(\"{id}\")]<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">public<\/span> IEnumerable&lt;Employee&gt; GetEmployees(<span style=\"color: #333399; font-weight: bold;\">int<\/span> Id)<br \/>        {<br \/>            <span style=\"color: #333399; font-weight: bold;\">var<\/span> Emp = <span style=\"color: #008800; font-weight: bold;\">from<\/span> emp <span style=\"color: #008800; font-weight: bold;\">in<\/span> GetEmployees()<br \/>                      <span style=\"color: #008800; font-weight: bold;\">where<\/span> emp.Id.Equals(Id)<br \/>                      <span style=\"color: #008800; font-weight: bold;\">select<\/span> emp;<br \/>            <span style=\"color: #008800; font-weight: bold;\">return<\/span> Emp.ToList&lt;Employee&gt;();<br \/>        }<br \/><\/pre>\n<\/div>\n<p>Listing 4 : GetEmployees() method based on input id<\/p>\n<p><b><\/p>\n<h3>7. Set up the POST method<\/h3>\n<p><\/b><br \/>The POST method is used in an API to create a new resource.<br \/>In this case, the POST method is AddEmployee and it is supposed to create a new employee.<br \/>Here the method is AddEmployee as defined in Listing 5 and it take no parameter.<br \/>Copy and paste this code in your EmployeeController file <\/p>\n<p><i>Note: We have not implemented a method to insert a new Employee to any database. In the next Tutorial we would actually write the code to handle data storage. But we have written it in such a way that it would run successfully and return the correct status code.<\/i><br \/><!-- HTML generated using hilite.me --><br \/><!-- HTML generated using hilite.me -->Also note that the employee parameter is given annotation <span style=\"color: blue;\">[FromBody] <\/span>which means that the modified data is coming from the body of the REST  request.<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<pre style=\"line-height: 125%; margin: 0;\">        <span style=\"color: #888888;\">\/\/METHOD TO ADD A NEW EMPLOYEE<\/span><br \/><span style=\"color: #0000cc;\">        [HttpPost]<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">public<\/span> ActionResult <span style=\"color: #0066bb; font-weight: bold;\">AddEmployee<\/span>([FromBody]Employee employee)<br \/>        {<br \/>            <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Created<\/span>(Request.QueryString.Value, employee);<br \/>        }<br \/><\/pre>\n<\/div>\n<p>Listing 5: The AddEmployee POST method<\/p>\n<p><b><\/p>\n<h3>8. Write the PUT&nbsp; and DELETE method<\/h3>\n<p><\/b><br \/>The PUT method is used to modify an existing resource.<br \/>Here we have named our PUT method as EditEmployee()<\/p>\n<p>Notice that the PUT method takes two parameters:<br \/>Id, which represents the Id of the Employee to be edited.<br \/>Employee object containing the modified data to be used to update the record<\/p>\n<p>Also note that the employee parameter is given annotation <span style=\"color: blue;\">[FromBody] <\/span>which means that the modified data is coming from the body of the REST request. (this is explained in Tutorial 4)<\/p>\n<p>We would cover the concept of header and body in Tutorial 4, when we would test this REST API with the POST and PUT operations.<\/p>\n<p>The DELETE method is similar to the PUT method except that:<br \/>the DELETE method takes only one parameter<br \/>the DELETE method deletes an Employee record<br \/>the DELETE method is annotated with the HttpDelete annotation<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<pre style=\"line-height: 125%; margin: 0;\">        <span style=\"color: #888888;\">\/\/METHOD TO EDIT AND EMPLOYEE<\/span><br \/><span style=\"color: #0000cc;\">        [HttpPut(\"{Id}\")]<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">public<\/span> ActionResult <span style=\"color: #0066bb; font-weight: bold;\">EditEmployee<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span> Id, [FromBody]<span style=\"color: #333399; font-weight: bold;\">string<\/span>  employee)<br \/>        {<br \/>            <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Ok<\/span>(<span style=\"background-color: #fff0f0;\">\"Employee with the Id: \"<\/span> + Id + <span style=\"background-color: #fff0f0;\">\" have beeen updated!\"<\/span> );<br \/>        }<br \/><br \/><br \/>        <span style=\"color: #888888;\">\/\/METHOD TO DELETE AND EMPLOYEE<\/span><br \/><span style=\"color: #0000cc;\">        [HttpDelete(\"{Id}\")]<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">public<\/span> ActionResult <span style=\"color: #0066bb; font-weight: bold;\">DeleteEmployee<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span> Id, [FromBody]<span style=\"color: #333399; font-weight: bold;\">string<\/span> employee)<br \/>        {<br \/>            <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Ok<\/span>(<span style=\"background-color: #fff0f0;\">\"Employee with the Id: \"<\/span> + Id + <span style=\"background-color: #fff0f0;\">\" have beeen deleted!\"<\/span>);<br \/>        }<br \/><\/pre>\n<\/div>\n<p>Listing 6: The PUT and DELETE methods<\/p>\n<p>Now copy and paste the PUT and DELETE codes into the EmployeeController file<br \/>The final result of the EmployeeController file is given in Figure 6<\/p>\n<p><span style=\"color: red;\">Note: <\/span>You may not have written the first part of the code, where I created 4 different employees manually. Since is did not provide this code. So go ahead and type it yourself<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/4.bp.blogspot.com\/-ajCArmsHfUs\/WrPzAgoppxI\/AAAAAAAABfQ\/JgG6hrJez8kK4JK0B6E1Y3-AxLn__wobwCLcBGAs\/s1600\/Figure%2B6%2B-%2BAll%2BController%2BFiles.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"953\" data-original-width=\"1600\" height=\"380\" src=\"https:\/\/4.bp.blogspot.com\/-ajCArmsHfUs\/WrPzAgoppxI\/AAAAAAAABfQ\/JgG6hrJez8kK4JK0B6E1Y3-AxLn__wobwCLcBGAs\/s640\/Figure%2B6%2B-%2BAll%2BController%2BFiles.jpg\" width=\"640\" \/>&nbsp;<\/a><\/div>\n<div style=\"clear: both; text-align: center;\">Figure 6: Final result of the EmployeeController File <\/div>\n<p>If you have come this far, congratulation!! You have done noble! \ud83d\ude04\ud83d\ude04\ud83d\ude04<br \/>But it is not yet over.<br \/>You could go ahead to run the program but at this point. We need to do more to test this program than just runing it. So read the Next Steps section.<\/p>\n<p><b><\/p>\n<h3>9. Next Steps<\/h3>\n<p><\/b><br \/>Congratulations again for coming this far! \ud83d\ude03\ud83d\ude03\ud83d\ude03<br \/>Since you need to learn how to use a REST Client application (ARC in this case, from Tutorial 1) to test REST APIs, you need to learn how to test all the operations GET, POST, PUT, DELETE.<br \/>In Tutorial 4, you will learn how to specify the input values for creating a new record. You will also learn how to speficy the content type, the header, body and other parameters<\/p>\n<p>For now thanks you for reading!<br \/>Do leave a comment below if this have been informative for you.<\/p>\n<div style=\"text-align: center;\"><a href=\"https:\/\/www.youtube.com\/channel\/UCvHgEAcw6VpcOA3864pSr5A\" target=\"_blank\" rel=\"noopener noreferrer\">Video Lessons on YouTube<\/a><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we would focus on creating the POST method, sending POST request and retrieving a response. Content Review of Tutorials 1 and 2 &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[289],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/124"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"predecessor-version":[{"id":1756,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/124\/revisions\/1756"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}