How To Test ASP.NET Core 5 Web API Service With Swagger

Introduction

 
In this article I will cover the complete procedure of How to test web API Service using Swagger Open API It is the simplest and very beautiful way to test and document your web service.
 
Swagger (Open API) is a language-agnostic specification for describing and documenting the REST API. Swagger Allows both the Machine and Developer to understand the working and capabilities of the Machine without direct access to the source code of the project the main objectives of swagger (Open API) are to: 
  • Minimize the workload to connect with Microservice.
  • Reduce the Time Needed to accurately document the Microservice.

How to Test API Web Service in Asp Net Core 5 Using Swagger

 
We are going to test the following use case for this article. In this article I am going to test the POST, GET, PUT, DELETE, AND UPDATE, Endpoints of Web Service.
 
 
Here in this example, we are going to test the Web Service using Swagger Developed using the Asp.Net Core 5 Web API.
 
 

Test POST Call in Swagger

 
Here we are going to send the JSON object with the Following Properties
  1. {  
  2.   "id": 0,  
  3.   "userName""string",  
  4.   "userPassword""string",  
  5.   "userEmail""string",  
  6.   "createdOn""2021-04-16T20:38:41.062Z",  
  7.   "isDeleted"true  
  8. }  
 
Now click the try-out button.
 
 
If we want to Create the New Person in the Database, we must send the JSON Object to the End Point then data is posted by Swagger UI to POST End Point in the Controller then Create Person Object Creates the Complete Person Object in the Database.
 
 
Now you can see that our API gives us the server response == true so it indicates that a new record has been created in the database against our JSON Object that we have sent using Swagger.
 

Test DELETE Call in Swagger

 
For Deletion of Person, we have a separate End Point Delete Employee from the Database, we must send the JSON Object to the End Point then data is posted by Swagger UI to call the End Point DELETE Person in the Controller then Person Object will be deleted from the Database.
 
 
Now we are going to delete the record against a certain Id
 
 
We are going to delete the record against the given email if the API service equal to true then our record deleted from the database successfully.
 
 
In the above two pictures, you can see that after hitting the Delete Endpoint our server response is true it indicates that our record has been deleted from the database against our desired Email.
 

Test GET Call in Swagger

 
If we want to get the record of all the personal data from the database, we will hit the GET endpoint in our Web Service using swagger after the completion of the server request all the data will be given to us by our GETAllPerson Endpoint.
 
 
Now we are going to fetch all the record from the database
 
 
After the successful execution of Web service, all the record is visible to us in the form of a JSON object in database we have Only One Record so that
 

Test PUT Call in Swagger

 
In put-call, we are going to update the record in the database. We have Separate End Points for All the operations like Update the Record of the Person by User Email from the Database by Hitting the UPDATE End Point that sends the request to the controller and in the controller we have a separate End Point for Updating the record based on User Email from the database. 
 
We are going to update the given below JSON Object in the database
 
 
  1. {  
  2.   "id": 0,  
  3.   "userName""Mudassar Ali Khan",  
  4.   "userPassword""17859345@",  
  5.   "userEmail""[email protected]",  
  6.   "createdOn""2021-04-16T20:41:15.443Z",  
  7.   "isDeleted"false  
  8. }  
To given below JSON Object
  1. {  
  2.   "id": 0,  
  3.   "userName""Sardar Mudassar Ali Khan",  
  4.   "userPassword""17859345@",  
  5.   "userEmail""[email protected]",  
  6.   "createdOn""2021-04-16T20:41:15.443Z",  
  7.   "isDeleted"false  
  8. }  
After the successful submission of data, you can see that our server response is true it indicates that our data has been successfully submitted to the database
 
 
 

Summary

 
Swagger (Open API) is a language-agnostic specification for describing and documenting the REST API. Swagger Allows both the Machine and Developer to understand the working and capabilities of Machine without direct access to the source code of the project the main objectives of swagger (Open API) are too,
  • Minimize the workload to connect with Microservice.
  • Reduce the Time Needed to accurately document the Microservice.
Swagger is the Interface Description Language for Describing the RESTful APIS Expressed using JSON. Swagger is used to gathering with a set of open-source software tools to Design build documents and use Restful Web Services Swagger includes automated documentation code generation and test the generation. Three main components of Swashbuckle Swashbuckle.AspNetCore.Swagger is the Object Model and Middleware to expose swagger Document as JSON End Points.
 
Swashbuckle.AspNetCore.SwaggerGen.Swagger Generator that builds Swagger Document Objects Directly from your routes controllers and models this package is combined with swagger endpoints middleware to automatically expose the Swagger JSON. Swashbuckle.AspNetCore.SwaggerUI is an Embedded version of the Swagger UI Tool it explains swagger JSON to build a rich customizable practical for explaining the Web API Functionality it includes a built-in test harnessed for the public Methods.
 

Conclusion

 
Now My Conclusion about the Swagger Open API Swagger is the best Open API for Documenting the Restful API As a Developer I have great experience with Swagger for testing the restful API sending the Complete JSON Object and then getting the response in a professional way. It is easy to implement in Asp.net core Web API Project and after the configuration, Developers can enjoy the beauty of Swagger Open API.


Similar Articles