What Is The Easiest And Quickest Way To Test An Application's Web APIs

There are lots of tools that you can use to test an application's API Controller.

Some of the good ones are,

But, the simplest and the easiest way to test an application's API is to use Windows PowerShell or Visual Studio Package Manager Console. I will show you step-by-step how to use PowerShell or PM (Package Manager Console) to test the application Web APIs.

We need a simple Web API project so that we can test and debug some of the sample application APIs. Let’s start downloading a simple To-do project from GitHub.

Download and run the below TodoMvcSolution from below link.

https://github.com/SmartITAz/TodoMvcSwaggerSolution

ASP.NET Core

Here are the APIs we can test; Get, Post, Put, and Delete for this sample application.

ASP.NET Core

Here are the Web APIs we want to test.

  1. //Copyright 2017 (c) SmartIT. All rights reserved.  
  2. //By John Kocer  
  3. // This file is for Swagger test, this application does not use this file  
  4. using System.Collections.Generic;  
  5. using Microsoft.AspNetCore.Mvc;  
  6. using SmartIT.Employee.MockDB;   
  7.   
  8. namespace TodoAngular.Ui.Controllers  
  9. {  
  10.   [Produces("application/json")]  
  11.   [Route("api/Todo")]  
  12.   public class TodoApiController : Controller  
  13.   {  
  14.     TodoRepository _todoRepository = new TodoRepository();  
  15.   
  16.     [Route("~/api/GetAllTodos")]  
  17.     [HttpGet]  
  18.     public IEnumerable<SmartIT.Employee.MockDB.Todo> GetAllTodos()  
  19.     {  
  20.       return _todoRepository.GetAll();  
  21.     }  
  22.   
  23.     [Route("~/api/AddTodo")]  
  24.     [HttpPost]  
  25.     public SmartIT.Employee.MockDB.Todo AddTodo([FromBody]SmartIT.Employee.MockDB.Todo item)  
  26.     {  
  27.       return _todoRepository.Add(item);  
  28.     }  
  29.   
  30.     [Route("~/api/UpdateTodo")]  
  31.     [HttpPut]  
  32.     public SmartIT.Employee.MockDB.Todo UpdateTodo([FromBody]SmartIT.Employee.MockDB.Todo item)  
  33.     {  
  34.       return  _todoRepository.Update(item);  
  35.     }  
  36.   
  37.     [Route("~/api/DeleteTodo/{id}")]  
  38.     [HttpDelete]  
  39.     public void Delete(int id)  
  40.     {  
  41.       var findTodo = _todoRepository.FindById(id);  
  42.       if (findTodo != null)  
  43.         _todoRepository.Delete(findTodo);  
  44.     }  
  45.   }  
  46. }  

Note

Your local port number may be different than ours. Use your local port number.

  • http://localhost:63274/api/GetAllTodos // GET
  • http://localhost:63274/api/AddTodo //POST
  • http://localhost:63274/api/UpdateTodo //PUT
  • http://localhost:63274/api/DeleteTodo/5 // DELETE

PowerShell commands for above HTTP APIs

  • Testing the GET Operations
    Invoke-RestMethod http://localhost:63274/api/GetAllTodos -Method GET

  • Testing the POST Operations
    Invoke-RestMethod http://localhost:63274/api/AddTodo -Method POST -Body(@{id="0"; name="Call Boss-OK"  } | ConvertTo-Json) -ContentType "application/json"

  • Testing the PUT Operations
    Invoke-RestMethod http://localhost:63274/api/UpdateTodo -Method PUT -Body(@{id="4"; name="Call Boss-DONE"  } | ConvertTo-Json) -ContentType "application/json"

  • Testing the DELETE Operations
    Invoke-RestMethod http://localhost:63274/api/DeleteTodo/5 -Method DELETE
Start a PowerShell window

From the Windows Search, type PowerShell and select the "Windows PowerShell" application.

ASP.NET Core

ASP.NET Core

Testing GET with PS

Compile a Get command for PowerShell and paste onto the command line. (It is one continuous line).

Invoke-RestMethod http://localhost:63274/api/GetAllTodos -Method GET

Response

Windows PowerShell

Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\Users\Admin> Invoke-RestMethod http://localhost:63274/api/GetAllTodos -Method GET

id name

-- ----

 1 Check eMails

 2 Do dishes

 3 Call Boss

 4 Call Boss-DONE

PS C:\Users\Admin>

Testing POST with PS

Compile a POST command for PowerShell and paste on to command line. (It is  one continuous line)
We are composing a Body with our Todo item {id="0"; name="Call Boss-OK" } 

Invoke-RestMethod http://localhost:63274/api/AddTodo -Method POST -Body(@{id="0"; name="Call Boss-OK"

  } | ConvertTo-Json) -ContentType "application/json"

Response

We received Id=4 with an Inserted Todo Item like below

PS C:\Users\Admin> Invoke-RestMethod http://localhost:63274/api/AddTodo -Method POST -Body(@{id="0"; name="Call Boss-OK"

  } | ConvertTo-Json) -ContentType "application/json"

id name

-- ----

 4 Call Boss-OK

PS C:\Users\Admin>

Testing PUT with PS: Compile a PUT command for PowerShell and paste on to command line. (It is  one continuous line)
We are composing a Body with our Todo item {id="0"; name="Call Boss-OK" }

Invoke-RestMethod http://localhost:63274/api/UpdateTodo -Method PUT -Body(@{id="4"; name="Call Boss-DONE"  } | ConvertTo-Json) -ContentType "application/json"

Response

We received name= Call Boss-DONE with an Updated Todo Item like below

PS C:\Users\Admin> Invoke-RestMethod http://localhost:63274/api/UpdateTodo -Method PUT -Body(@{id="4"; name="Call Boss-D

ONE"  } | ConvertTo-Json) -ContentType "application/json"

id name

-- ----

 4 Call Boss-DONE

PS C:\Users\Admin>

Testing DELETE with PS: Compile a DELETE command for PowerShell and paste on to command line.
We are calling  with our API item Id=4 to delete.

Invoke-RestMethod http://localhost:63274/api/DeleteTodo/4 -Method DELETE

Response

We received no info back like below

PS C:\Users\Admin> Invoke-RestMethod http://localhost:63274/api/DeleteTodo/4 -Method DELETE

PS C:\Users\Admin>

NOTE

We can do all this within Visual Studio Package Manager Console instead of PowerShell.

To use Package Manager Console, open the PM console and paste the compiled GET method like below screen capture.

ASP.NET Core

How to debug an application API? 

We can put a breakpoint on the Visual Studio API and then, invoke that API from PowerShell or Package Manager Console to debug it.

Debugging when you calling an application API

ASP.NET Core

This will complete the testing and the debugging of the Web API.

Summary

In this article, we learned how to test and debug Web APIs. You can download source code from GitHub.

Thank you for reading this article. You may be interested in reading the below training articles too.

Resources

Books

https://www.amazon.com/s/ref=dp_byline_sr_ebooks_1?ie=UTF8&field-author=John+Kocer&search-alias=digital-text&text=John+Kocer&sort=relevancerank

Visual Studio Download Site

https://www.visualstudio.com/downloads/

Node.js installation page

https://nodejs.org/en/download/

Postman Download

https://www.getpostman.com/

SmartIT.Employee.MockDB Download

https://www.nuget.org/packages/SmartIT.Employee.MockDB/

SmartIT.Payment.MockDB Download https://www.nuget.org/packages/SmartIT.Payment.MockDB/