How to Test ASP.NET Core Web API using Postman?
In this article, I am going to discuss How to Test ASP.NET Core Web API using Postman. Please read our previous article where we discussed How to Create, Build, Run, and Test ASP.NET Core Web API Applications. In fact, this is a continuation part to our previous article. A Postman is a client tool used for testing Restful APIs.
Postman in ASP.NET Core Web API Project:
Testing a Web API project with Postman and Swagger is a common practice in the software development process. Postman and Swagger are both powerful tools that allow you to interact with and test your APIs. In this article, I will explore How to use Postman and in our upcoming article, I will explore How to Test ASP.NET Core Web APIs using Swagger. Here’s a step-by-step guide on how to test your Web API project using Postman:
- Install Postman: If you haven’t already, download and install Postman from the official website (https://www.postman.com/downloads/).
- Launch Postman: Open the Postman application on your computer.
- Import your API Documentation: If you have an OpenAPI specification (formerly known as Swagger specification) for your API, you can import it into Postman. Click on “Import” in the Postman application and choose the option to import from a file. Select your OpenAPI specification file (usually in JSON or YAML format) and import it into Postman.
- Explore the API Documentation: Once imported, you can explore your API documentation within Postman. It will list all the available endpoints, request methods (GET, POST, PUT, DELETE, etc.), request parameters, headers, and response structures.
- Send Requests: To test your API, select an endpoint from the documentation, choose the appropriate HTTP method, and click the “Send” button. Postman will send the request to the specified URL with the provided parameters.
- Inspect the Response: Postman will display the response received from the API. You can examine the status code, response headers, and the response body. You can also use Postman’s built-in tools to format and validate the response data.
How to Test ASP.NET Core Web API using Postman?
Now let us see, how to test ASP.NET Core Web API using Postman. First open Postman. Once you open the Postman, then click on the New button as shown in the below image to create a new request.
Once you click on the New button, it will open the following Create New window. Here, simply click on the Request tab as shown in the below image.
Once you click on the Request button, it will open the below Save Request window. Here, you need to do a couple of things. First, give a meaningful name to your API Request and provide a description of the API. Then you need to create one collection where you can create any of the Requests or you can use an existing collection to save this request. As we are doing for the first name, so, I am creating a new collection as shown in the below image.
Once you click on the right arrow button, the collection will be created and the Save button is renamed with the collection name, and also that button is now enabled. Simply click on that button as shown in the below image.
Once you click on the Save to Collection button, it will launch the following window.
Now let us understand the different components of the above Request.
Selecting HTTP Method:
You need to select the appropriate method of the API that you want to access. Here, in the dropdown, you can find the list of available HTTP Methods. As per your need, you need to select GET, POST, PUT, PATCH, DELETE, etc. In our example, the WeatherForecast API is of type GET, so, here we need to select the GET HTTP Method.
Enter the Request URL:
In the Enter Request URL text, you need to provide the API URL that you want to access as shown in the below image. As we are going to access the WeatherForecast API, So, I enter the URL as https://localhost:5001/WeatherForecast.
Params: If you want to send any parameters then you need to set those parameters as key-value pairs in the Params tab. In our example, we not going to pass any parameters to the Web API, so we keep this empty.
If your Web API needs some kind of authorization, then you need to provide such authorization here. In the type tab, you need to select the appropriate authorization and you also need to provide the value for the same. In our example, we don’t require any authorization, so we also keep this tab empty.
The next one is the Headers tab. As you can see, the postman by default sends the following headers to the Web API. If your API needs some additional headers, then you can set those headers here only in the form of Key-Value Pairs. In our example, the API does not require any additional headers. So, we are only going with the default headers.
If your API accepts some data in the body, then you can send such data in the body tab. Further depending on the type of data you can choose form-data, raw, binary, etc as shown in the below image. As our API is a GET request, as it doesn’t require any data, none is selected by default.
Now, click on the Send button, which will send the request to the Web API server as shown in the below image.
Once you click on the Send button, it will make a request to the Web API and the Web API sends the response back to the Client. And you can see the response in the response body as shown in the below image.
In the response, you can also check the HTTP status code, the time it takes, and the size of the data it received as shown in the below image.
This is how you can use Postman to test your Web APIs. In the next article, I am going to discuss How to Create, Build, Run, and Test an ASP.NET Core Web API Project using Visual Studio. Here, in this article, I try to explain How to use Postman to test ASP.NET Core Web APIs and I hope you enjoy this Postman in ASP.NET Core Web API article.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.