How to Test ASP.NET Core Web API Using Postman

Testing ASP.NET Core Web API Using Postman

In this article, I will discuss How to Test ASP.NET Core Web API Using Postman. Please read our previous article discussing How to Create, Build, Run, and Test ASP.NET Core Web API Applications. In fact, this is a continuation of our previous article. A Postman is a client tool used for testing Restful APIs.

Postman in ASP.NET Core Web API Project:

Testing Web API services 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:

  1. Install Postman: If you haven’t already, download and install Postman from the official website (https://www.postman.com/downloads/).
  2. Launch Postman: Open the Postman application on your computer.
  3. 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.
  4. 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.
  5. 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.
  6. 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 do you 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, click on the New button, as shown in the image below, to create a new request.

How to Test ASP.NET Core Web API using Postman?

Once you click on the New button, it will open the following Create New window. Here, click on the Request tab as shown in the below image.

How to Test ASP.NET Core Web API using Postman?

Once you click on the Request button, it will open the Save Request window below. Here, you need to do a couple of things. First, give your API request a meaningful name 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 do for the first name, I am creating a new collection, as shown in the image below.

How to Test ASP.NET Core Web API using Postman?

Once you click on the right arrow button, the collection will be created, and the Save button will be renamed with the collection name. Also, that button will now be enabled. Click on that button as shown in the below image.

How to Test ASP.NET Core Web API using Postman?

Once you click on the Save to Collection button, it will launch the following window.

How to Test ASP.NET Core Web API using Postman?

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. 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 we need to select the GET HTTP Method here.

Selecting HTTP Method

Enter the Request URL:

In the Enter Request URL text, you must provide the API URL you want to access, as shown in the image below. As we will access the WeatherForecast API, I entered the URL as https://localhost:5001/WeatherForecast.

Enter the Request URL

Params: If you want to send any parameters, you need to set those parameters as key-value pairs in the Params tab. In our example, we will not pass any parameters to the Web API, so we keep this empty.

Authorization:

If your Web API needs authorization, you must 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 authorization, so we keep this tab empty.

authorization

Headers:

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.

Headers

Body:

If your API accepts some data in the body, then you can send such data in the body tab. Further, depending on the data type, you can choose form-data, raw, binary, etc, as shown in the image below. As our API is a GET request, as it doesn’t require any data, none is selected by default.

Body

Now, click the Send button, which will send the request to the Web API server, as shown in the image below.

How to Test ASP.NET Core Web API using Postman

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. You can see the response in the response body, as shown in the image below.

How to Test ASP.NET Core Web API using Postman

In the response, you can also check the HTTP status code, the time it takes, and the size of the data it receives, as shown in the below image.

How to Test ASP.NET Core Web API using Postman

This is how you can use Postman to test your Web APIs. In the next article, I will 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.

2 thoughts on “How to Test ASP.NET Core Web API Using Postman”

Leave a Reply

Your email address will not be published. Required fields are marked *