All about API: Using HTTP Methods

All about API: Using HTTP Methods

Before reading this article, I would recommend reading the following previous part.

Introduction

In this article we will see a simple Web API project with HTTP methods like GET, POST, PUT and DELETE.

  • GET - Used to read from the list.
  • POST - Used to add in the list.
  • PUT - Used to modify/ update the element in the list.
  • DELETE - Used to delete the element from the list.

Now let’s see step by step implementation of all methods:

  • Open Visual Studio Editor.

  • Select ‘FILE’ menu, expand ‘New’ and click on ‘Project…’

image.png

  • Expand ‘Visual C#’ and select ‘Web’ from the left panel.?

  • Select ‘ASP.NET Web Application’.

  • Provide appropriate name of the application and select the location.

  • Click on ‘OK’ button.

image.png

  • Select ‘Empty’ as a template and check ‘Web API’ checkbox.

  • Press ‘OK’ button and it will create an empty Web API project.

image.png

  • Right click on ‘Controllers’, expand ‘Add’ and click on ‘Controller…’

image.png

  • Select ‘Web API 2 Controller with read/write actions’ and press ‘Add’ button, which will add all HTTP methods.

image.png

  • Provide appropriate name and press ‘Add’ button.

image.png

  • Controller is created with read/write actions,

image.png

Now let’s create List of string and do CRUD operations on it.

GET – Returns entire list

GET with ID – Returns value associated with the ID

POST – Adds one value in the list

PUT – Updates existing value

DELETE – Deletes existing value

image.png

In my previous article, I mentioned that ‘api’ in the route template is just the convention, without that also we can execute the application. Let’s remove that here and verify.

image.png

Now in order to test all HTTP methods, we need to download REST Client. Download link is as follows:

addons.mozilla.org/en-US/firefox/addon/rest..

image.png

  • Expand ‘Tools’ menu and click on ‘REST Client’ which will open REST Client in firefox.

image.png

Run your application and follow below steps:

GET

a. Select ‘GET’ as Method.

b. Copy URL and press ‘SEND’ button.

c. Observe the response, we can see two string elements are added in the list.

image.png

GET By ID

a. In order to get the individual string element from the list, pass index in the URL.

b. Check the response, you will have individual element associated with the index.

image.png

POST

a. Select ‘POST’ as method.

b. Add Content-Type header with the value ‘application/json’.

c. Add the value which you want to add in the list in Body and press ‘SEND’ button.

image.png

d. Call once again the GET method and you can see that preceding element is added in the list.

image.png

PUT

a. Select ‘PUT’ as method and add index for which you want to modify in the URL

b. Add Content-Type header with value ‘application/json’.

c. Add updated value in the body and press ‘SEND’ button.

image.png

d. Call GET method once again and you can see in the response that preceding value is updated.

image.png

DELETE

a. Select ‘DELETE’ as method and add index for which you want to delete from the list in URL.

b. Press ‘SEND’ button.

image.png

c.Call once again GET method and you can see in the response that the given index element is deleted from the list.

image.png

Read more articles on Web API:

Next >> All About API: HTTP Response Message - Part Three