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
Here are the APIs we can test; Get, Post, Put, and Delete for this sample application.
Here are the Web APIs we want to test.
-
-
-
- using System.Collections.Generic;
- using Microsoft.AspNetCore.Mvc;
- using SmartIT.Employee.MockDB;
-
- namespace TodoAngular.Ui.Controllers
- {
- [Produces("application/json")]
- [Route("api/Todo")]
- public class TodoApiController : Controller
- {
- TodoRepository _todoRepository = new TodoRepository();
-
- [Route("~/api/GetAllTodos")]
- [HttpGet]
- public IEnumerable<SmartIT.Employee.MockDB.Todo> GetAllTodos()
- {
- return _todoRepository.GetAll();
- }
-
- [Route("~/api/AddTodo")]
- [HttpPost]
- public SmartIT.Employee.MockDB.Todo AddTodo([FromBody]SmartIT.Employee.MockDB.Todo item)
- {
- return _todoRepository.Add(item);
- }
-
- [Route("~/api/UpdateTodo")]
- [HttpPut]
- public SmartIT.Employee.MockDB.Todo UpdateTodo([FromBody]SmartIT.Employee.MockDB.Todo item)
- {
- return _todoRepository.Update(item);
- }
-
- [Route("~/api/DeleteTodo/{id}")]
- [HttpDelete]
- public void Delete(int id)
- {
- var findTodo = _todoRepository.FindById(id);
- if (findTodo != null)
- _todoRepository.Delete(findTodo);
- }
- }
- }
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.
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.
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
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/