How To Use Postman With ASP.NET Core Web API Testing

Manual Testing with Postman

ASP.NET Core Testing with Postman

If you are a developer, tester, or a manager, sometimes understanding various methods of API can be a challenge when building and consuming the application.

Generating good documentation and help pages for your Web API using Postman with .NET Core is as easy as making some HTTP calls.

Let’s start downloading simple To-do projects from GitHub.

  1. Download and run the below TodoMvcSolution from this link.

    ASP.NET Core
  1. Download Postman
    Postman is a Google Chrome application for testing API calls. You can download and install Postman from below web site.

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

Swagger UI 

Here are the Web APIs we want to test.

//Copyright 2017 (c) SmartIT. All rights reserved.  
//By John Kocer  
// This file is for Swagger test, this application does not use this file  
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 mine. 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

Testing GET with Postman

  • Testing GET is very easy. First, we need to set HTTP Action from the drop-down list as GET.
  • Then, we need to type or paste into the API URL box.
  • Then, click the blue SEND button.

If the GET is successful, we see the status: 200 OK. 

Testing GET with Postman

Testing POST with Postman

  • First, we need to set Http Action from the dropdown list as POST.
  • Then, we need to type or paste into the API URL box.
  • AddTodo API accepts a Todo object in JSON format. We need to pass a new Todo JSON data.
  • To pass JSON data we need to Select Body Tap.
  • Select the Raw
  • Select JSON(Application/JSON) as text format.
  • Write or paste your Todo JSON data.
  • Then, click the blue SEND button.

If the POST is successful, we see the status: 200 OK.

You will see Status:200 for success and the return value in the Return Body tab. We sent Publish Postman Todo item with id=0 and we received id=5 as result.

Testing POST with Postman

Testing PUT with Postman

  • First, we need to set HTTP Action from the dropdown list as PUT.
  • Then, we need to type or paste into the API URL.
  • UpdateTodo API accepts a Todo object in JSON format. We need to pass an existing Todo JSON data.
  • To pass JSON data we need to Select Body Tab
  • Select the Raw format
  • Select JSON(Application/JSON) as text format.
  • Write or paste your Todo JSON
  • Then click the blue SEND

If the PUT is successful, we see the status: 200 OK.  

You will see Status:200 for success and the return value in the Return Body Tab. We sent Publish Postman Todo item with "name": "Publish Postman-In progress" and we receive an updated todo result.

Testing PUT with Postman

Testing DELETE with Postman

  • First, we need to set Http Action from the dropdown list as DELETE.
  • Then, we need to type or paste into the API URL box.
  • DeleteTodo/5 API accepts an id on the  We need to pass an existing Todo with an Id value.
  • Then, click the blue SEND button.

If the Delete is successful, we see the status: 200 OK.

Testing DELETE with Postman

This will complete the Postman Part 1 tutorial. In "Part 2- Automated testing with Postman", we will look into automating Postman API testing and writing a script to run continuous integration test case.

Summary

In this article, we learned how to use Postman with ASP.NET Core Web APIs. Download source code from GitHub.

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