- Stateless type
- Enable to leverage a cache of data
- Full leverages a layered format system
- Full leverages a uniform data interface
Let's see one image that gives a good explanation of HTTP verbes.
Now, I will show you an examples of HTTP GET, POST, PUT, DELETE in WebAPI.
HTTP GET
This verb should be used only to get information or data from database or other source. The code will look like the given code.
-
-
-
-
- [HttpGet]
- [SwaggerOperation(Tags = new[] { "xyz" })]
- [Route("GetData")]
- public IHttpActionResult GetData()
- {
- var list = _provider.GetData();
-
- if (list == null)
- {
- return NotFound();
- }
- return Ok(list);
- }
Inside this code, top 1-4 number lines show swagger implimentation and after that HTTP Verb, and line number 6 displays swagger class to create folder to categorized APIs. [Route] is attribute to change endpoint via attribute routing in WebAPI and complete route will look like this. You can set
RoutePrifix(/api/Dictionary/)
http://local.xxxxxxx.com/api/Dictionary/GetData
There is one more line of code that may confuse you. _provider is just a class object which is used to call in business layer.
HTTP POST
This verb should be used only to post or create new entry with information or data to database or other source. Code will look like the given below.
-
-
-
-
-
- [HttpPost]
- [SwaggerOperation(Tags = new[] { "Dictionary" })]
- [Route("SaveData")]
- public IHttpActionResult SaveData(CustomerInfo obj)
- {
- ResponseWrapper responseObj = new ResponseWrapper();
- try
- {
-
- var _result = _provider.SaveData(obj);
- if (_result != null)
- {
- responseObj.status = "success";
- responseObj.data = new { message = _result };
- }
- else
- {
- responseObj.status = "warning";
- responseObj.data = new { message = "Operation Failed." };
- }
- }
- catch (Exception ex)
- {
- responseObj.status = "error";
- responseObj.data = new { message = ex.Message };
- }
- return Ok(responseObj);
- }
In this part of code, I have written the code to post or save data into database using REST verb HTTP POST. Final endpoint will look like the given line.
http://local.xxxxxxx.com/api/Dictionary/SaveData HTTP PUT
This verb should be used only to update the existing entry with information or data to database or other source. The code will look like the given code.
-
-
-
-
-
- [HttpPut]
- [SwaggerOperation(Tags = new[] { "Dictionary" })]
- [Route("UpdateData")]
- public IHttpActionResult UpdateData(CustomerInfo obj)
- {
- ResponseWrapper responseObj = new ResponseWrapper();
- try
- {
- var _result = _provider.UpdateData(obj);
- if (_result != null)
- {
- responseObj.status = "success";
- responseObj.data = new { message = _result };
- }
- else
- {
- responseObj.status = "warning";
- responseObj.data = new { message = "Operation Failed." };
- }
- }
- catch (Exception ex)
- {
- responseObj.status = "error";
- responseObj.data = new { message = ex.Message };
- }
- return Ok(responseObj);
- }
In this part of code, I have written the code to update information if data is already there in database, using REST verb HTTP PUT. The final endpoint will look like the given line.
http://local.xxxxxxx.com/api/Dictionary/UpdateData
HTTP DELETE
This verb should be used only to delete existing entry within database or other source. Code will look like this.
-
-
-
-
-
- [HttpPut]
- [SwaggerOperation(Tags = new[] { "Client Dictionary" })]
- [Route("DeleteData/{Id}")]
- public IHttpActionResult DeleteData(int Id)
- {
- ResponseWrapper responseObj = new ResponseWrapper();
- try
- {
- var _result = _provider.DeleteData(Id);
- if (_result != null)
- {
- responseObj.status = "success";
- responseObj.data = new { message = _result };
- }
- else
- {
- responseObj.status = "warning";
- responseObj.data = new { message = "Operation Failed." };
- }
- }
- catch (Exception ex)
- {
- responseObj.status = "error";
- responseObj.data = new { message = ex.Message };
- }
- return Ok(responseObj);
- }
In this part of code, I have written the code to delete information if data already exists in database, using REST verb HTTP DELETE. Final endpoint will look like the given line.
http://local.xxxxxxx.com/api/Dictionary/DeleteData/6
Above endpoint is passing Id as URL-encoded.
HTTP PATCH
The HTTP PATCH type should be used to update any partial resources. This verb should be used only to update the existing entry with partial data in database or other source.
-
-
-
-
-
- [HttpPatch]
- [SwaggerOperation(Tags = new[] { "Dictionary" })]
- [Route("UpdateData/{Id}")]
- public IHttpActionResult UpdateData(int Id)
- {
- ResponseWrapper responseObj = new ResponseWrapper();
- try
- {
- var _result = _provider.UpdateData(Id);
- if (_result != null)
- {
- responseObj.status = "success";
- responseObj.data = new { message = _result };
- }
- else
- {
- responseObj.status = "warning";
- responseObj.data = new { message = "Operation Failed." };
- }
- }
- catch (Exception ex)
- {
- responseObj.status = "error";
- responseObj.data = new { message = ex.Message };
- }
- return Ok(responseObj);
- }
In this part of the code, I have written the code to update partial information if data already exists in database, using REST verb HTTP PATCH. The final endpoint will looks like the given line.
http://local.xxxxxxx.com/api/Dictionary/DeleteData/6
The above endpoint is passing Id as URL-encoded.
Other references