Tuesday, July 23, 2019

Setup Swagger for ASP.NET WebAPI documentation

this blog demonstrates step by step to add swagger in web api project and will submit  http request GET/POST/PUT via swagger UI.

Swagger is API developer tools for the Open API Specification(OAS), enabling development across the entire API life cycle, from design and documentation, to test and deployment.

Definition -
Swagger is a simple yet powerful representation of your RESTful API. With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability.”

To add Sweggar into WEBAPI Project, you need to perform only 2 steps

1. Install SwashBuclkle open source project via Nuget.

Install SwashBuclkle Nuget Package

 2. Configure Swagger Config File :
         after package installed, you will find a new file ‘SwaggerConfig.cs’ under App_Start Folder

    
Swagger Config

public class SwaggerConfig
    {
        public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;

            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {             
                        c.SingleApiVersion("v1""DemoWebAPI");                 
                    })
                .EnableSwaggerUi(c =>
                    {
                        c.DocumentTitle("Demo Web API");

                    });
        }
    }

build and run webAPI project and browser http://localhost:65096/swagger

Swagger UI documentation















Swagger allow you can submit HTTP request GET, HTTP POST, HTTP PUT against Web API

Example - Submit HTTP GET Request For /api/Projects/{projectId}

steps to perform web api testing in SOAPUI

this blog will demonstrates the steps by steps to perform web api testing in SOAPUI.

SoapUI is built for end-to-end testing of Rest and SOAP Apis, Web Service and Micro Service.  SoapUI can easily automated API testing and perform load testing.


Header :
X-API-UserName :
X-API-Token :

In above API, we need to pass Order ID as part of API‘s Path and for API security verification, we have to pass user Name and API token as HTTP Request Header.

1.     Install SOAPUI Tool
2.     Run the SoapUI app and click on REST Icon

SOAP UI


3.     Enter REST API URL  -  https://localhost:/Order/ID/1000

SOAPUI - REST API URL


4.     Click on Header Tabs and enter Header Name and Value ( in example, X-API-Token Header)  

SOAP UI - ADD HEADER
         
Click on OK

SOAP UI -  HEADER


5.     Click on Run Button () to submit HTTP Request for API

SOAPUI - Submit HTTP REQUEST