All about API: Using HTTP Methods - Part Two

Before reading this article, I would recommend reading the following previous part.
 
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…’

    File

  • 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.

    OK

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

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

    Empty

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

    Controllers

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

    Add

  • Provide appropriate name and press ‘Add’ button.

    Add

  • Controller is created with read/write actions,
    code
  • 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

    code
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.

code

Now in order to test all HTTP methods, we need to download REST Client. Download link is as follows:
 
 
Download  
  • Expand ‘Tools’ menu and click on ‘REST Client’ which will open REST Client in firefox.

    Tools
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.

    get

  • 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.

     GET By ID

  • 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.

    POST

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

    POST

  • 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.

    PUT

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

    PUT

  • DELETE

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

    b. Press ‘SEND’ button.

    SEND

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

    SEND
Read more articles on Web API:


Similar Articles