Before going to explain the significant verbs or methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE and CONNECT) for the communication among different clients and servers, first of all, we should understand what are HTTP methods? and how they are useful in real time.
What are HTTP Methods?
When a browser submits a request to a server, the request contains some part of an HTTP related information. The requested part is what the client would like the server to do some action with the specified resource. The HTTP methods represents those actions.
Example
- Retrieve specific data from a server (GET, HEAD)
- Submit data to a server (POST)
- Update data in a data source (PUT)
- Delete an item from the server’s data source (DELETE)
When implementing these HTTP methods, we should be aware that the software represents the user in their actions and allows the user to be aware of any actions they might take have an unexpected significance to themselves or others.
It is not possible to ensure that the server doesn’t generate any side effects as a result while performing a request. Basically, the end user did not request the side effects. So, they cannot be held accountable for them.
First, we have to understand one of the important properties of these methods before going too much deeper into these verbs or methods. i.e “Idempotent”.property.
What is Idempotent?
A sequence that never has side effects is called idempotent. That means no concurrent operations are being executed on the same set of resources. Methods can also have the property of “idempotence”. The methods GET, HEAD, PUT and DELETE have this property. The side effects of ‘N’ number of identical requests is the same as for a single request. And the methods OPTIONS and TRACE should not have side effects.
Now, let's move on to the actual HTTP methods for getting a clear picture on what we are talking about.
HTTP GET
The GET method is used to retrieve data from a server using a given requested URI. The GET method should only retrieve data and should have no other impact on the data.
Example
For better understanding, I implemented GET method in Web API Controller using Entity Framework.
- public class EmployeeController : ApiController
- {
- [HttpGet]
- public IHttpActionResult GetAllEmployees(){
- using (var context = new EmployeeDBEntities())
- {
- employees = context.Employees
- .Select(e => new EmployeeViewModel()
- {
- Id = e.EmpID,
- FirstName = e.EmployeeName,
- Designation = e.Designation
- }).ToList<EmployeeViewModel>();
- }
- if (employees.Count == 0)
- {
- return NotFound();
- }
- return Ok(employees);
- }
- }
The above example GetAllEmployees() method returns all the employees using EF. If no employee exists, then it will return 404 Not found otherwise it will return 200 OK response with employee data. Here, EmployeeViewModel is a POCO class with some properties. IHttpActionResult is an ActionResult for a request. We have other type of ActionResults like void, HttpResponseMessage, and other custom types etc. We will cover these types in some other article. The HTTPGet attribute decorator clearly saying that it will not allow any other HTTP verbs for the above method except GET method.
The GET method is called from client with some ajax call with a sample URL like below,
- Uri format: http://serverUrl:serverPort/api/controllerName
- Sample Url: http://localhost:12003/api/employee
The GET method can be useful in real time with the following important considerations.
- We should not have same HTTP methods with the same number of parameters with the same type.
- We can request the URL multiple times with no side effect. i.e Clicking on a hyperlink which redirects to another page has no side effect. This gives a chance to the browser to catch the response for faster retrieval.
- The method GET is not secure. So, it is not suitable for transferring confidential data. You can just pass some configuration data or session id. The method is useful for retrieving content from a server.
- The data that needs to be sent to the server is not large and can be a maximum length of URL supported by all browsers.
HTTP POST
A post request is used to send complex objects/entities/uploading file etc to the server. In POST method data is sent as part of message body instead of sending as part of URL string to the server.
Example




Roni AxelradPosted Sep 16, 2018, 9:32 AM
The put example you added doesn't do what you write it does. It does not create a new resource in case it doesn't exist
Nagaraj SPosted Jul 18, 2017, 11:15 AM
Good one. Expecting more articles from you
Kashif AsifPosted Jul 18, 2017, 12:40 AM
Nice article keep it up (y)