HTTP PUT, POST And DELETE In ASP.NET Web API

Overview

In this article, we will learn about HTTP Verbs - PUT, POST, and DELETE with example

Introduction

When we talk about database row, there are four actions that we perform on it.

  • C-Create a Row,
  • R-Read a Row,
  • U- Update a Row, and
  • D-Delete a Row

In the context of an ASP.NET Web API resource, these four actions correspond to GET, PUT, POST, and DELETE. Now, let’s understand the concepts which are related to these verbs.

  • Request Verbs (GET, POST, AND DELETE)
    These describe what should be done with the resource. For more details on these verbs, you can refer this URL - 
    https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

  • Request Header
    When a client sends a request to the server, the request contains a header and a body. The request method contains additional information, such as - what type of response is required. For example: The response should be in XML, JSON, or some other format.

  • Request Body
    Request body contains the data that you want to send to the server. For example, a post request contains the data for a new item that you want to create. The data format may be in XML or in JSON.

  • Response Body
    The response body contains the data of the response from the server. For example, if the request is for a specific product, the response body includes product details in XML, JSON, and so on.

  • Response Status Codes
    These are the HTTP status codes that give the client details on the status of the request. Some of the common status codes are 404 not found, 204 No content, and so on.

We use a tool called "Fiddler" to monitor these Verbs. Now, let’s see our Valuescontroller class and will include the list of strings.


Here, I have passed three values.

Now, return the list of strings in s=our static variable.



We have another overloaded GET version which has an ID as parameter. We can use this ID as a GET parameter index. We want to pass id 0 that will return the index position that is at 0. If we pass 1, it will return index position 1, and so on.



We use POST method to create a new item.


Notice, this method has a value parameter which is of type string. Whatever we are passing here, we want to add it to our static variable.



We use PUT method to update an item.


This method has two parameters, the id and the new value which we want to update it with. So, here we will use id as an index and we want to update that item at that index position as,



We use the DELETE method to remove an item.

So, we will use id to remove an item at the specified position.


Now, save the changes and run the app.


As you can see from the above output, we have got three values. Now, let’s launch fiddler.



On the fiddler, notice that the response we are getting is in XML. Now, drag and drop the request on tab. You will see the following screen.





The request that is issued is GET, and the second is URI to which we have issued that request. Now, let’s look for POST request and create a new item.

|So, click on the dropdown.



Now, select Post in which specify the string in JSON format. As we are passing the string in JSON format, we need to specify the content type: application/json 


Now, click on Execute.



Double click on the URL and notice the status code we are getting back.



It is HTTP /1.1 204 No content. It's just a POST request that we had added in a new item collection. Because there is nothing to return, it is throwing "204 No content" error.

Now, let’s add a static variable. For that, go back to Composer tab. Now, issue the GET request and execute that request.



When you click on Response Header tab, you will see the following.



In Response Header, the HTTP status code that we got is "200 OK". What we are getting back is XML.

Now, we will see PUT action and let's update that string.



Select PUT action. As we are passing JSON to the server, specify the content type. We want the new string to be updated string.


And specify the index position here as /1 and execute.



Again, we have got HTTP status code "204 No content available". Go and change  the GET request, delete the JSON string data, and execute the GET request. We get the following.



Now, let’s check for DELETE request.

We want to delete the item which is at index position 1. Let's execute it.



Now, look for the header tab.



Again, we get "HTTP 204 No content".

Now, let’s go back to our composer tab and change to GET. 



Now, if you look at these three methods, PUT, POST, and DELETE, the return type is void and that’s the reason we get the HTTP status code as "204 No content". We have complete control on what status code should be returned with ASP.NET Web API.

Conclusion - So, this was all about HTTP verbs in ASP.NET Web API. Hope this article was helpful.